1

I've got a 'conversations' collection in MongoDB which I'm querying from NodeJS to use the returned data to render the conversation's page.

The data has been stored in the database correctly as far as I can see, when I query it everything comes back as I'd expect, apart from a couple of nested objects - the two users that the conversation belongs to.

Here's what I get when I console.log a conversation (note the 'participants' field:

[ { _id: 57f96549cc4b1211abadf28e,
    __v: 1,
    messages: [ 57f96549cc4b1211abadf28d ],
    participants: { user2: [Object], user1: [Object] } } ]

In Mongo shell the participants has the correct info - the id and username for both participants.

Here's the Schema:

var ConversationSchema = new mongoose.Schema({
  participants: {
      user1:
        {
          id: String,
          username: String
        },
      user2:
        {
          id: String,
          username: String
        },
    },
  started: Number,
  messages: [
    {
      type: mongoose.Schema.Types.ObjectId,
      ref: "Message"
    }
  ]
});

Here's the creation of the conversation document:

var conv = {
  participants : {
    "user1" : {
      "id" : req.body.senderId,
      "username" : req.body.senderName
    },
    "user2" : {
      "id" : req.body.recipientId,
      "username" : req.body.recipientName
    }
  },
  created : Date.now(),
  messages : [] // The message _id is pushed in later.
}
Conversation.create(conv, function(err, newConvo){
  if(err){
    console.log(err);
  } else {
    newConvo.messages.push(newMessage);
    newConvo.save();
  }
})

And lastly, in case it's useful, here's the query to Mongo:

// view all conversations a user belongs to
app.get('/messages', function(req, res){
  Conversation.find({
    $or : [
      {"participants.user1.id" : req.user._id},
      {"participants.user2.id" : req.user._id}
    ]
  }, function(err, convos){
    if(err){
      console.log('Error getting Convos ' + err)
    } else {
      res.render('messages', {convos: convos, currentUser: req.user});
    }
  });
});

Thanks a lot for any help that!

2 Answers 2

1

It seems that everything is alright, the console.log just doesn't print nested objects by default. Try using:

console.log(JSON.stringify(conversation))

When logging a conversation in order to see the participants objects.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot Andresk. You're right, when I stringify it, I get the data I'm expecting. Still, when I pass it to the client side, I can't do anything with it. Do I have to stringify it and put it back into a JS object before passing it to the client?
I see from your other answer that you fixed, great :)
0

Fixed it!

Andresk's answer above was a big shove in the right direction. As he said, everything was OK, but I wasn't accessing the returned object in the correct way. It's obvious now, but I wasn't providing the index number for the 'convos' object.

I simply needed to do this, even though I was only getting one 'conversation' document back from MongoDB:

console.log(convos[0].participants.user1.username);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.