流星找到一个带有 id 的对象 [英] Meteor finding an object with id
问题描述
假设我有一个类似于 557fba5a8032a674d929e6a1
的 id 字符串,它存储在会话中.我尝试检索 _id
与上述相同的文档,但即使它存在也找不到它.
Assume I have an id string that looks like 557fba5a8032a674d929e6a1
which is stored in session. I try to retrieve a document whose _id
is same as above, but I fail to find it even though it exists.
Posts.findOne({_id: "557fba5a8032a674d929e6a1"});
返回未定义.现有对象如下所示:
returns undefined. The existing object looks like following:
我可以做到这一点
var id = "557fba5a8032a674d929e6a1";
var posts = Posts.find().fetch();
var post = _.filter(posts, function (post) { return post._id._str === id });
return post
但它看起来很脏.这是我的控制台输入和输出,用于进一步调查此行为(Posts == Applicant
).你会发现,虽然我们要找的文件肯定存在,但我找不到.
but it seems dirty. Here's my console inputs and outputs to further investigate this behavior (Posts == Applicants
). You will notice that even though the document that we are looking for definitely exists, I can't find it.
推荐答案
您必须将 _id 定义为 Mongo.ObjectID
来表示 ObjectID 输入正确.
You have to define _id as a Mongo.ObjectID
to represent the ObjectID type correctly.
Posts.findOne({_id: new Mongo.ObjectID("557fba5a8032a674d929e6a1") });
使用 Meteor 的 ObjectID 的一个警告是它们的时间戳不正确.特别是当它们插入客户端时.
One caveat of ObjectIDs with Meteor is their timestamps aren't correct. Particularly if they're inserted on the client.
这篇关于流星找到一个带有 id 的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!