流星尝试创建ObjectID时返回无效的十六进制字符串错误? [英] Meteor returns invalid hexadecimal string error trying to create ObjectID?
问题描述
我正在向数据库中插入一些东西,并使用jQuery在其他地方更新它(给我一个休息时间,我是新来的),之后我需要能够单击它并显示一些UI东西,这意味着ID.因此,我设置了ID变量,然后尝试,然后尝试在click事件中使用它:
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:
Uncaught Error: Invalid hexadecimal string for creating an 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()
来告诉Meteor使用Mongo样式ID. ,或者(可能更容易)如果newMessageID
的值是insert()
返回的_id
,则仅使用newMessageID
而不是new Mongo.ObjectID(newMessageID)
.
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()
.
这篇关于流星尝试创建ObjectID时返回无效的十六进制字符串错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!