Search code examples
node.jsmongoosetingodbnosql

Mongoose findById returning null for first object when the object exists


Mongoose is returning null for the first object, _ID 0, in the DB even though it exists. I can retrieve the first post just fine using similar code, but I can not retrieve the first tag. I can retrieve all of my data with the exception of the first Tag, tag1 for the first post. If I add a second post with tags: tag4, tag5 and tag6 I can retrieve all of the data for the second post including tag4. If I point to tag1 in a later post it cannot be retrieved.

I have done some of searching and know that null being returned means that the record can't be found. I have also tried using find and findOne in addition to findById and get the same result. I can't figure out where I'm going wrong if the record exists. I'm sure there is probably a better way of doing it.

Thanks for the help.

I'm using mongoose, mongoose-simpledb, and tungus with node-webkit.

Save Tags:

$.each(tagArray, function(i, t) {
    var tags = db.Tags();
    tags.nextCount(function(err, count) {
        post.tags.push(count + i); //tag ID to post
    });
    tags.text= tagArray[i]; //tag text
    post.nextCount(function(err, count) {
        tags.posts.push(count); //post ID to tag
    });
    tags.save(function(err) {
        if (err) {throw err;}
        console.log('tag saved');
    });
});

Find Tags:

$.each(results[i].tags, function(j, t) {
    db.Tags.findById(results[i].tags[j], function(err, tag) {
        if (err) {return console.error(err);}
        if (!tag) {return console.log('Could not find tag...');}
        postContent += '<a href="">' + tag.text + '</a> ';
    });
});

Or if I use the following tag2 is returned instead of tag1 which is what should be returned.

db.Posts.
    find({}).
    populate('tags').
    exec(function(error, post) {
        console.log(post[0].tags[0].text);
    });

Tags Model

var ObjectId = require('mongoose-simpledb').Types.ObjectId;

exports.schema = {
    _id: Number,
    text: String,
    posts: [{type: ObjectId, ref: 'Posts', index: true}]
};

Tags DB

{"k":"0000000078","o":"0000000061","v":"001"}
{"_id":0,"_uid":1,"_dt":1409869458919,"_s":"c245621efdb176d3f4dd2db749590730"}
{"_id":0,"text":"Tag1","posts":[{"$wrap":"$oid","v":0}],"__v":0}
{"k":"0000000078","o":"0000000061","v":"001"}
{"_id":1,"_uid":1,"_dt":1409869458921,"_s":"80bc4453ee777de0177b27ba76ddc859"}
{"_id":1,"text":"Tag2","posts":[{"$wrap":"$oid","v":0}],"__v":0}
{"k":"0000000078","o":"0000000061","v":"001"}
{"_id":2,"_uid":1,"_dt":1409869458930,"_s":"12682b1c27ea57e8c09b87a2a6605510"}
{"_id":2,"text":"Tag3","posts":[{"$wrap":"$oid","v":0}],"__v":0}

Using find on the Tags DB:

db.Tags.findOne({
    _id: '0'
}, function(err, result) {
    if (err) return console.error(err);
    console.dir('findOne: ' +result.text);
    //throws error - Cannot read property 'text' of null
});

db.Tags.
findById(0, function(err, tag) {
    if (err) return console.error(err);
    console.log('tag by id: ' + tag);
    //returns - null
});

db.Tags.
find({}).
exec(function(error, tag) {
    console.log("find:" + tag[0]);
    //returns - ( _id: 0, text: 'Tag1', _v: 0, posts: [0] )
    //correctly finding the tag but not by id
});

If I change these find functions to find any tag other than the first tag, the correct information is returned.


Solution

  • The issue turned out to be mongoose-simpledb. I'm not sure if the issue is a bug in mongoose-simpledb or a conflict with tungus, but the "same" code works fine once mongoose-simpledb is removed.