流星 0.5.7:如何处理/使用 Meteor.Collection.ObjectID? [英] meteor 0.5.7: how to handle/use Meteor.Collection.ObjectID?

查看:50
本文介绍了流星 0.5.7:如何处理/使用 Meteor.Collection.ObjectID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天更新了我的流星并尝试使用新的 Meteor.Collection.ObjectID.但因为没有成功.首先,我以这种方式更新了我的收藏:

I have updated my meteor yesterday and tried using the new Meteor.Collection.ObjectID. But since with no success. First i updated my collections in this way:

myCollection = new Meteor.Collection('mycollection', {idGeneration: 'MONGO'}现在,普通的新插入有一个 _idWi2RmR6CSapkmmdfn... (?)

myCollection = new Meteor.Collection('mycollection', {idGeneration: 'MONGO'} Now, normal new inserts have an _id like Wi2RmR6CSapkmmdfn... (?)

然后我有一个包含数组的集合.我喜欢为这个数组中的每个对象都有一个唯一的 id.所以我 $push 一个带有像 id: new Meteor.Collection.ObjectID() 字段的对象到我的数组中.数据库中的结果是这样的:ObjectId("5b5fc278305d406cc6c33756").(这似乎很正常.)

Then i have a collection with an array included. I like to have an unique id for every object in this array. So i $push an object with a field like id: new Meteor.Collection.ObjectID() into my array. The result in the database is like this: ObjectId("5b5fc278305d406cc6c33756"). (This seems to be normal.)

但稍后我想更新我推送的对象,如果 id 等于一个 id,我之前将其作为数据属性存储在 html 标签中.

But later i want to update my pushed object, if the id equals an id, which i stored as data attribute in an html tag before.

var equals = EJSON.equals(dbId, htmlId); (这每次都会导致 false.所以我记录了值 dbIdhtmlId 使用 console.log(typeof dbId, dbId);)

var equals = EJSON.equals(dbId, htmlId); (This results every time in false. So i logged the values dbId and htmlId into the console with console.log(typeof dbId, dbId);)

这两个变量的值如下:

object { _str: 'a86ce44f9a46b99bca1be7a9' } (dbId)

string ObjectID("a86ce44f9a46b99bca1be7a9") (htmlId;这似乎是正确的,但为什么自定义类型是字符串?)

string ObjectID("a86ce44f9a46b99bca1be7a9") (htmlId; this seems to be correct, but why is a custom type a string?)

如何正确使用Meteor.Collection.ObjectID?

推荐答案

htmlId 放入 html 时,需要将其作为字符串而不是对象放入,请记住 _id 现在是一个对象,handlebars 正在猜测并使用 toString() &这就是为什么它显示为 ObjectID("...").

When placing your htmlId in your html you need to put it in as a string and not as an object, remember _id is an object now, handlebars is guessing and using toString() & thats why it shows up as ObjectID("...").

因此,如果您在 html 中使用 {{_id}},您现在需要使用 {{_id.toHexString}} 来正确提取出来

So if you're using {{_id}} in your html you now need to use {{_id.toHexString}} to properly extract the string part of it out

当您使用 javascript 提取此 html 值时,您需要将其重新转换为 objectid:

When you extract this html value with your javascript you need to make it back into an objectid:

js:

var valuefromhtml = "a86ce44f9a46b99bca1be7a9"; //Get with Jquery,DOM,etc

htmlId = new Meteor.Collection.ObjectID(valuefromhtml); //see: http://docs.meteor.com/#collection_object_id

EJSON.equals(htmlId, dbId); //Should be true this time    

这篇关于流星 0.5.7:如何处理/使用 Meteor.Collection.ObjectID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆