0

I have a schema called Message and it has a property replies which are also Message objects. I am trying to define it in mongoose, but replies keeps returning undefined.

var MessageSchema = new Schema({
    sender: {
        type: Schema.ObjectId,
        ref: 'User'
    },
    replies: [{type:Schema.ObjectId, ref:'Message'}],
    roomId: String,
    sendTime: Date,
    content: String,
    parentId: Schema.ObjectId
});

I've also tried replies: [MessageSchema] and replies:[Schema.ObjectId] but all of them keep returning undefined.

2
  • What do you mean by "all keep returning"? Are you trying to create new items or do you have an existing collection that you are trying to use this with? In either case you either have an array of ObjectID values or an array containing actual objects depending on whether you are intending to reference or embed the data. Commented Mar 2, 2015 at 3:18
  • Sorry, "all keep returning" meant all my different attempts returned undefined. I'm trying to reference the data, so I want replies to store just ObjectIds. Commented Mar 2, 2015 at 3:21

1 Answer 1

1

Nothing wrong with the code you are listing so it's what you are not showing us that causes the problems.

When you reference you are going to both save a copy of the object in the collection and also add that related ObjectId value to the array of objects that are in "reply" in your case.

There are several ways to do it, but a good safe MongoDB way is using $push to add additional items.

As a complete example:

var async = require('async'),
    mongoose = require('mongoose'),
    Schema = mongoose.Schema;


var userSchema = new Schema({
  "name": String
});

var messageSchema = new Schema({
  "sender": { "type": Schema.Types.ObjectId, "ref": "User" },
  "replies": [{ "type": Schema.Types.ObjectId, "ref": "Message" }],
  "roomId": String,
  "sendTime": { "type": Date, "default": Date.now },
  "content": String,
  "parentId": Schema.Types.ObjectId
});

var Message = mongoose.model( "Message", messageSchema );
var User = mongoose.model( "User", userSchema );

mongoose.connect('mongodb://localhost/test');

async.waterfall(
  [
    // Clean up samples
    function(callback) {
      async.each(
        [User,Message],
        function(model,callback) {
          model.remove({},callback);
        },
        callback
      )
    },

    // Create user
    function(callback) {
      User.create({ "name": "Bill" },callback);
    },

    // Create a message
    function(user,callback) {
      Message.create({
        "sender": user._id,
        "roomId": "1",
        "content": "message"
      },function(err,message) {
        callback(err,user,message);
      });
    },

    // Create a reply
    function(user,message,callback) {
      Message.create({
        "sender": user._id,
        "roomId": "1",
        "content": "reply",
        "parentId": message._id
      },callback);
    },

    // Save that reply on the parent
    function(message,callback) {
      Message.findByIdAndUpdate(
        message.parentId,
        { "$push": { "replies": message._id } },
        function(err,message) {
          console.info( message );
          callback(err);
        }
      );
    },

    // List that
    function(callback) {
      Message.find({},function(err,messages) {
        if (err) callback(err);

        console.log(
          "All:\n%s",
          JSON.stringify( messages, undefined, 4 )
        );
        callback();
      });
    }

  ],
  function(err) {
    if (err) throw err;
    mongoose.disconnect();
  }
);

And the output:

{ _id: 54f3dff198a8c85306a2ef67,
  sender: 54f3dff198a8c85306a2ef66,
  roomId: '1',
  content: 'message',
  __v: 0,
  sendTime: Mon Mar 02 2015 14:58:41 GMT+1100 (AEDT),
  replies: [ 54f3dff198a8c85306a2ef68 ] }
All:
[
    {
        "_id": "54f3dff198a8c85306a2ef67",
        "sender": "54f3dff198a8c85306a2ef66",
        "roomId": "1",
        "content": "message",
        "__v": 0,
        "sendTime": "2015-03-02T03:58:41.387Z",
        "replies": [
            "54f3dff198a8c85306a2ef68"
        ]
    },
    {
        "_id": "54f3dff198a8c85306a2ef68",
        "sender": "54f3dff198a8c85306a2ef66",
        "roomId": "1",
        "content": "reply",
        "parentId": "54f3dff198a8c85306a2ef67",
        "__v": 0,
        "sendTime": "2015-03-02T03:58:41.393Z",
        "replies": []
    }
]

That way if you called .populate() on an message that has "replies" present, it will go back to the collection and retrieve the related data and make it appear that data was part of that item as well.

Please not that such magic does not happen "recursively" without your own intervention. It's just a basic helper, so if you want more then you still have to do the lifting yourself.

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

2 Comments

Thanks for the reply, I'm going to try it out. Is async.waterfall necessary?
@Carpetfizz No it's just a demonstration and a simple way to pass through values without nesting code blocks.

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.