4

I've looked up other questions including this one however I couldn't find an answer resolves my problem.

I have defined my models in the same way described in official documentation of mongoose by showing references and defining types of models as Schema.Types.ObjectId. Here they are:

story_model.js

var storySchema = new Schema({
    ...
    candidateParts: [{ type: Schema.Types.ObjectId, ref: 'StoryPart'}],
    ...
}, { usePushEach: true});

storyPart_model.js

var storyPartSchema = new Schema({
    ...
    storyId: { type:Schema.Types.ObjectId, ref: 'Story' },
    ...
}, { usePushEach: true});

And, I have built a query like that where ObjectID = require('mongodb').ObjectID; and storyModel is Story passed through storyController.js

Here is how I call populate:

StoryController.prototype.getCandidateParts = function (storyId, callback) {
var me = this;
const id = { '_id': ObjectID(storyId) };
me.storyModel.findOne(id).populate('candidateParts').exec(function(err,story) {
    if (err) return callback(err, new me.ApiResponse({ success: false, extras: { message: me.ApiMessages.DB_ERROR }}));
    if (story) {
        return callback(err, story);
    }else {
        return callback(err, new me.ApiResponse({ success: false, extras : { message: me.ApiMessages.STORY_NOT_EXISTS }}));
    }
});
};

Following is how I check results of my new populated story:

me.getCandidateParts(story._id, (err, populatedStory) => {
        if (err) {
            return;
        }
        if (populatedStory) {
            console.log(populatedStory);

But I get this result instead of story with populated parts:

[ { _id: 5a8bfaab78798703c0760bd1,
__v: 2,
is_updated: 2018-02-20T10:38:32.620Z,
is_created: 2018-02-20T10:38:32.620Z,
candidateParts: []
... } ]

By the way, I'm sure candidateParts include in database something like that:

ObjectId("5a8bfa33cd601903b57329ab")

And yes, I'm also sure that I have defined model references with exactly the same identifiers.

Thanks in advance!

4
  • If you remove the .populate('candidateParts') part, does the result logged in console.log(populatedStory) have anything in the candidateParts array? Commented Feb 20, 2018 at 12:03
  • @chridam yes, id of part returns like that [ 5a8c2df95eb65602b02e0687 ] Commented Feb 20, 2018 at 14:19
  • And if you query the storyparts collection in mongo shell with that id e.g db.storyparts.findOne({ _id: ObjectId("5a8c2df95eb65602b02e0687") }) do you get any document as a result? Commented Feb 20, 2018 at 14:23
  • @chridam I have found I had same ID for StoryPart and Story instances. I should have something wrong during saving new parts. Commented Feb 20, 2018 at 14:40

1 Answer 1

2

I realized that somehow I have same ID for StoryPart and Story instances. That's why it returns nothing.

EDIT:

I have resolved the problem by ensuring and correcting IDs of story and storypart. I do not delete the question since there might be people facing the same problem, so just check your ObjectIDs if they refer to correct instances.

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

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.