admin管理员组文章数量:1420981
mongoose.mongo.Types.ObjectId does not have fromString or fromHexString functions. It seems that new mongoose.mongo.Types.ObjectId(hexString) does not create an object id either.
var id = new mongoose.Types.ObjectId(hexString);
db.Record.find({_id:id }, function (err, campaign){
if(err) console.log(err);
callback(campaign);
});
mongoose.mongo.Types.ObjectId does not have fromString or fromHexString functions. It seems that new mongoose.mongo.Types.ObjectId(hexString) does not create an object id either.
var id = new mongoose.Types.ObjectId(hexString);
db.Record.find({_id:id }, function (err, campaign){
if(err) console.log(err);
callback(campaign);
});
Share
Improve this question
asked Apr 28, 2015 at 16:40
swoggerswogger
1,1191 gold badge14 silver badges30 bronze badges
4
-
And what's
hexString
? ObjectId generally accepts hexadecimal strings ? – adeneo Commented Apr 28, 2015 at 16:43 -
2
I would venture to guess that your problem isn't that
campaign
is null, but rather that it's returning an array and you're trying to access the properties of the document against the array, not a single document. Change your query to usefindById
. If that's not the case, what's the value ofid
before the query is executed? – Brian Shamblen Commented Apr 28, 2015 at 17:56 - returns empty array. Tired find one and findById still no success. – swogger Commented Apr 29, 2015 at 10:20
- @adeneo - hex string: '553f8a4286f5c759f36f8e5b' – swogger Commented Apr 29, 2015 at 10:21
1 Answer
Reset to default 6I finally found the method you're looking for. The mongoose.Types.ObjectId
class has a static function called createFromHexString
, which returns an instance of an ObjectId
.
var id = mongoose.Types.ObjectId.createFromHexString(hexString);
db.Record.findOne({_id: id}, function (err, campaign){
if(err) console.log(err);
callback(campaign);
});
本文标签: javascriptHow to create ObjectId in Mongoose 40x from Hex stringStack Overflow
版权声明:本文标题:javascript - How to create ObjectId in Mongoose 4.0.x from Hex string? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745312600a2653005.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论