These are chat archives for Automattic/mongoose

13th
Jan 2018
Kev
@lineus
Jan 13 2018 00:16
heres a gist of where it was a few days ago along with comments that include the console from node and a find command from mongodb.
did you change something since then?
AnGrYbEaRcOdE
@CodeME1118
Jan 13 2018 00:29
Since then, we are now learning to reference and not embed the data, but when I try to link the data, nothing shows up. Though if I embed it, then there is no issue.
But embedding it is problmatic
Kev
@lineus
Jan 13 2018 00:31
if you want to add the schemas to the gist I'll take a peak.
Kev
@lineus
Jan 13 2018 01:11
@CodeME1118 in the version that is on this gist in the Comment.create callback you need to add ._id to comment so that it only pushes the id.
Mike Botsko
@viveleroi
Jan 13 2018 01:28
It seems that schema.indexes() doesn't include the default _id_ index?
stherrienaspnet
@stherrienaspnet
Jan 13 2018 03:07
Hello everyone I have a simple question about reference key
when let say you have a user table and you have a userProfile table
on you userProfile table you have a userId field, should this field be type of ObjectId as user._id or string?
Sreekar Reddy
@sreekar2307
Jan 13 2018 04:01

@lineus
That didn't work
coffeshop.comments.push(comment._id);
weird part is it didn't even change the error that i was getting.

( UnhandledPromiseRejectionWarning: ValidationError: coffeshop validation failed: comments: Cast to [undefined] failed for value "[{"_id":"5a5981db5541e046aceddf05","comment":"Id occaecat laboris dolore commodo esse aliqua culpa enim incididunt tempor labore.","author":"Dolor","__v":0}]" at path "comments")

moreover I made a buffer comment and pushed it into coffeshop collection on my seed.js file may thats y ur seeing an id for your testdb on my db i could only view buffer comment not the one that i typed

Sreekar Reddy
@sreekar2307
Jan 13 2018 04:09
can u suggest any other way of adding comments to each coffeshop
Sreekar Reddy
@sreekar2307
Jan 13 2018 04:33
?????????
Sreekar Reddy
@sreekar2307
Jan 13 2018 04:44
see this anyone
https://gist.github.com/sreekar2307/904ab8c72c9d2cd655be7ee2b2462caf
error on line number 87 in app.js file
Nick Karnik
@theoutlander
Jan 13 2018 07:41
@stherrienaspnet it should be an ObjectId ref. You can use populate or populateVirtual when retrieving data without extra effort.
Kev
@lineus
Jan 13 2018 11:04
@sreekar2307 your seed.js is also trying to push the whole comment instance into the coffeeshop comments subdoc array. by adding ._id to the data.comments.push line, the behavior changed for me there too. I added a comment to your gist with my version of your seed.js and it's output.
Sreekar Reddy
@sreekar2307
Jan 13 2018 12:51
@lineus
that will never be an issue, because it will be triggered only once when we initially run the app when a user enters a comment
they will be directed to /coffeshops/:id which will render show.ejs.
moreover i completely changed schema of coffeshopSchema I directly embedded commentSchema without giving its reference inside it this is working fine.
I really appreciate your help.
https://gist.github.com/sreekar2307/904ab8c72c9d2cd655be7ee2b2462caf (updated)
Goutham Pulluri
@gpulluri
Jan 13 2018 13:59

hi, I am trying to update single nested sub document. Can you please take a look and tell me what I am doing wrong ?

bold Model

const mongoose = require('mongoose');

const PersonalInfoSchema = new mongoose.Schema({
  phoneNumber: String,
  gender: {
    type: String,
    enum: ['Male', 'Female', 'Other']
  },
  dateOfBirth: Date
});

const UserSchema = new mongoose.Schema({
  personalInfo: {PersonalInfoSchema}
}, {timestamps: true});

UserSchema.methods.toPersonalInfoJson = function () {
  return {
    personalInfo: this.personalInfo.toJSON()
  };
};

bold Server

router.post('/personalinfo', auth.required, function (req, res) {
  User.findOne({email: req.auth.email})
    .exec(function (findErr, user) {
      if (findErr || !user) return res.status(401).json({findErr: 'User not found!'});

      if (typeof req.body.phoneNumber !== 'undefined') {
        user.personalInfo.phoneNumber = req.body.phoneNumber;
      }
      if (typeof req.body.gender !== 'undefined') {
        user.personalInfo.gender = req.body.gender;
      }
      if (typeof req.body.dateOfBirth !== 'undefined') {
        user.personalInfo.dateOfBirth = req.body.dateOfBirth;
      }

      console.log(`updated user ${user}`);
      user.save(function (err) {
        if (err) {
          console.log(`signup error ${err}`);
          return res.status(422).json({error: 'update failed!'});
        }
        console.log(user.toPersonalInfoJson());
        return res.json({user: user.toPersonalInfoJson()});
      });
    });
});
Kev
@lineus
Jan 13 2018 14:10
@gpulluri are you getting an error?
or getting a 401 from express?
Goutham Pulluri
@gpulluri
Jan 13 2018 14:23
the subdocument is not getting updated
no error
there are obvious issues in the above code. I think mongoose docs are immutable and new fields would not saved etc. I am just looking for an example of how to update nested sub docs
@lineus didnt tag you in the above messages
Goutham Pulluri
@gpulluri
Jan 13 2018 14:45
@lineus figure out the problem personalInfo: {PersonalInfoSchema} .. I shouldn't have wrapped the schema in curly braces
Kev
@lineus
Jan 13 2018 14:47
nice!