Meteor 在尝试创建 ObjectID 时返回无效的十六进制字符串错误? [英] Meteor returns invalid hexadecimal string error trying to create ObjectID?
问题描述
我正在向数据库中插入一些内容并使用 jQuery 在其他地方更新它(让我休息一下,我是新手),之后我需要能够单击它并显示一些 UI 内容,这意味着获取ID.所以我设置了 ID 变量,然后尝试然后尝试在点击事件中使用它:
I'm inserting some stuff into the database and updating it elsewhere using jQuery (give me a break, I'm new), after which I need to be able to click it and display some UI stuff, which means getting the ID. So I set the ID variable and then try and and then try and use it in the click event:
...
"click .message-entry" : function (e) {
var id = this._id || new Meteor.Collection.ObjectID(newMessageId);
Session.set('singleMessageId', id);
},
...
原则上应该可以工作,但是我得到以下错误返回:
Should work in principle, however I get the following error returned:
未捕获的错误:用于创建 ObjectID 的十六进制字符串无效
断点显示ID既是字符串又是新插入文档的ID.这里可能出了什么问题?
A breakpoint reveals that the ID is both a string and the ID of the newly inserted document. What could be going wrong here?
推荐答案
默认情况下,Meteor 不使用 Mongo 的十六进制对象 ID.您可以通过将 {idGeneration: 'MONGO'}
传入 new MongoCollection()
,或者(可能更容易)只使用 newMessageID
而不是 new Mongo.ObjectID(newMessageID)
,如果newMessageID
的值是insert()
返回的_id
.
By default, Meteor doesn't use Mongo's hexadecimal object IDs. You could either tell Meteor to use Mongo style IDs by passing {idGeneration: 'MONGO'}
into new MongoCollection()
, or (probably more easily) just use newMessageID
instead of new Mongo.ObjectID(newMessageID)
, if the value of newMessageID
is the _id
returned by insert()
.
这篇关于Meteor 在尝试创建 ObjectID 时返回无效的十六进制字符串错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!