Node.js 当对象存在时,Mongoose findById为第一个对象返回null
Mongoose正在为数据库中的第一个对象返回null,即使它存在。我可以使用类似的代码检索第一篇文章,但是我无法检索第一个标记。我可以检索我的所有数据,除了第一个标记,第一篇文章的标记1。如果我添加了第二篇带有标记的文章:tag4、tag5和tag6,我可以检索第二篇文章的所有数据,包括tag4。如果我在后面的文章中指向tag1,它将无法检索 我做了一些搜索,知道返回null意味着找不到记录。除了findById之外,我还尝试使用find和findOne,得到了相同的结果。如果记录存在,我想不出哪里出了问题。我相信可能有更好的方法 谢谢你的帮助 我将mongoose、mongoose simpledb和tungus与节点webkit一起使用 保存标记:Node.js 当对象存在时,Mongoose findById为第一个对象返回null,node.js,mongoose,tingodb,nosql,Node.js,Mongoose,Tingodb,Nosql,Mongoose正在为数据库中的第一个对象返回null,即使它存在。我可以使用类似的代码检索第一篇文章,但是我无法检索第一个标记。我可以检索我的所有数据,除了第一个标记,第一篇文章的标记1。如果我添加了第二篇带有标记的文章:tag4、tag5和tag6,我可以检索第二篇文章的所有数据,包括tag4。如果我在后面的文章中指向tag1,它将无法检索 我做了一些搜索,知道返回null意味着找不到记录。除了findById之外,我还尝试使用find和findOne,得到了相同的结果。如果记录存在,我想不
$.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> ';
});
});
标签模型
var ObjectId = require('mongoose-simpledb').Types.ObjectId;
exports.schema = {
_id: Number,
text: String,
posts: [{type: ObjectId, ref: 'Posts', index: true}]
};
标签数据库
{"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.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
});
如果我更改这些查找函数以查找除第一个标记以外的任何标记,将返回正确的信息。问题原来是mongoose simpledb。我不确定问题是mongoose simpledb中的bug还是与tungus的冲突,但一旦删除mongoose simpledb,“相同”的代码就可以正常工作。这是一个令人困惑的问题。上面的标签是什么?它与
标记模型
架构不匹配,数据中存在重复和缺失的\u id
值。“标记数据库”是mongoose simpledb/tungus/TingoDB存储数据的json DB文件的内容。据我所知,数据库中没有重复的ID,这是一条记录,但我可能错了,因为这是我第一次使用mongoose simpledb/tungus/TingoDB。{“k”:“0000000078”,“o”:“0000000061”,“v”:“001”}{“id”:0,“uid”:1,“dt”:1409869458919,“s”:“c245621efdb176d3f4dd2db749590730”}{“id”:0,“文本”:“Tag1”,“posts”:[{“$wrap”:“$oid”,“v”:0}],“v”:0}
db.Posts.
find({}).
populate('tags').
exec(function(error, post) {
console.log(post[0].tags[0].text);
});