2014-09-04 59 views
1

貓鼬是第一個對象,即使它存在返回null,_ID 0,在DB。我可以使用類似的代碼檢索第一篇文章,但我無法檢索第一個標籤。除第一個標籤tag1外,我可以檢索所有數據。如果我添加標籤的第二個帖子:tag4,tag5和tag6,我可以檢索第二個帖子的所有數據,包括tag4。如果我在後面的帖子中指向tag1,則無法檢索它。貓鼬findById的第一個對象返回null時存在的對象

我已經做了一些搜索,知道正在返回null則表示該記錄不能被發現。我也嘗試使用find和findOne以及findById並獲得相同的結果。如果記錄存在,我無法弄清楚我要去哪裏。我相信可能有更好的方法來做到這一點。

感謝您的幫助。

我用貓鼬,鼬,SimpleDB的,和通古斯與節點的WebKit。

保存標籤:

$.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'); 
    }); 
}); 

查找標籤:

$.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> '; 
    }); 
}); 

或者,如果我使用下面的標籤2返回,而不是標籤1這是什麼應該被退回。

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

標籤型號

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

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

標籤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} 

使用發現在標籤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 
}); 

如果我更改這些發現功能來找到其他任何標記比第一個標籤,正確的英寸編隊返回。

+0

這是一個令人困惑的問題。上面的標籤數據庫是什麼?它與「標籤模型」模式不匹配,並且數據中存在重複和缺失的_id值。 – JohnnyHK 2014-09-05 12:39:42

+0

「Tags DB」是mongoose-simpledb/tungus/TingoDB存儲數據的json DB文件的內容。據我瞭解,它沒有重複的ID,這是db中的一條記錄,但我cusld是錯誤的因爲這是我第一次使用mongoose-simpledb/tungus/TingoDB。 { 「K」: 「0000000078」, 「O」: 「0000000061」, 「V」: 「001」} { 「_id」:0, 「_ UID」:1, 「_ DT」:1409869458919, 「_的」:」 c245621efdb176d3f4dd2db749590730 「} {」 _id 「:0,」 文 「:」 Tag1" 中, 「上崗」:[{ 「$包裹」: 「$ OID」, 「v」:0}], 「__ v」:0} – Mike 2014-09-05 18:57:16

回答

0

問題原來是mongoose-simpledb。我不確定問題是mongoose-simpledb中的問題還是與通古斯的衝突,但是一旦mongoose-simpledb被刪除,「相同」代碼就可以正常工作。