node.js 传入的参数必须是 24 个十六进制字符的字符串 - 我认为是

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30051236/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:43:54  来源:igfitidea点击:

Argument passed in must be a string of 24 hex characters - I think it is

node.jsmongodbmonk

提问by Carasel

I have a method to find a document in my database based on its ObjectID:

我有一种方法可以根据其 ObjectID 在我的数据库中查找文档:

      console.log('id: ' + id + ' type: ' + typeof id);
      collection.findOne({'_id':new ObjectID(id)}, function(error,doc) {
        if (error) {
          callback(error);
        } else {
           callback(null, doc);
        }
      });

When I run it I get the following error:

当我运行它时,我收到以下错误:

/myPath/node_modules/monk/node_modules/mongoskin/node_modules/mongodb/lib/mongodb/connection/base.js:245
    throw message;      
          ^
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at new ObjectID (/myPath/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:38:11)
at /myPath/collectionDriver.js:134:41

This refers to the collection.findOne()line above.

这是指collection.findOne()上面的行。

The console log I have before that call outputs the id as a string of 24 hex characters:

我在调用之前的控制台日志将 id 输出为 24 个十六进制字符的字符串:

id: "55153a8014829a865bbf700d" type: string

Before this I convert the id from an object to a string using JSON.stringify()but it appears to work successfully as shown in my console.log.

在此之前,我将 id 从一个对象转换为一个字符串,JSON.stringify()但它似乎成功地工作,如我的 console.log 所示。

Running db.myCollection.findOne({_id : ObjectId("55153a8014829a865bbf700d")})in Robomongo brings back the expected result.

db.myCollection.findOne({_id : ObjectId("55153a8014829a865bbf700d")})在 Robomongo 中运行带来了预期的结果。

回答by Carasel

The id that was passed in to my function was already an object ID in this case, so did not need a new ObjectID to be created from it.

在这种情况下,传递给我的函数的 id 已经是一个对象 ID,因此不需要从中创建新的 ObjectID。

When ObjectIDs are logged out to the console they appear as hex strings, rather than ObjectID("hexString"), so I thought I needed to convert it to do the find, but it was already in the format that I needed.

当 ObjectID 被登出到控制台时,它们显示为十六进制字符串,而不是ObjectID("hexString"),所以我认为我需要将它转换为查找,但它已经是我需要的格式。

回答by Martín Mtz

Try this:

尝试这个:

var hex = /[0-9A-Fa-f]{6}/g;
id = (hex.test(id))? ObjectId(id) : id;
collection.findOne({'_id':new ObjectID(id)}, function(error,doc) {
  if (error) {
    callback(error);
  } else {
    callback(null, doc);
  }
});

回答by profesoralex

In my case, this worked:

就我而言,这有效:

var myId = JSON.parse(req.body.id);
    collection.findOne({'_id': ObjectID(myId)}, function(error,doc) {
    if (error) {
      callback(error);
    } else {
       callback(null, doc);
    }
});

Don't forget to include at the beginning:

不要忘记在开头包含:

var ObjectId = require('mongodb').ObjectID;

回答by Talgat Medetbekov

Try out ObjectID(id)instead of new ObjectID(id)

尝试ObjectID(id)而不是new ObjectID(id)

回答by struensee

Yeah, I just spent thirty minutes baffled by this. If anyone finds themselves here, first check if you even need to convert to ObjectID in the first place. Like OP, I forgot that the hexstring is logged as just that - a hexstring.

是的,我只是花了三十分钟对此感到困惑。如果有人发现自己在这里,请首先检查您是否甚至需要首先转换为 ObjectID。像 OP 一样,我忘记了 hexstring 是这样记录的 - 一个 hexstring。