node.js 传入的参数必须是单个 12 字节的 String

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26453507/
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:01:04  来源:igfitidea点击:

Argument passed in must be a single String of 12 bytes

node.jsmongodbmongoose

提问by anish

mongoDB collection contains the following data

mongoDB 集合包含以下数据

db.stack.find()
{ "_id" : "8GieRu" }

The _id is not single String of 12 bytes,

_id 不是 12 字节的单个字符串,

As per the MongoDB document of [ObjectID][1], id (string) – Can be a 24 byte hex string, 12 byte binary string or a Number.

根据 [ObjectID][1] 的 MongoDB 文档,id (string) – 可以是 24 字节的十六进制字符串、12 字节的二进制字符串或数字。

Using Mongoose this collection is accessed using this Json

使用 Mongoose 使用此 Json 访问此集合

{"_id" : new mongoose.Types.ObjectId("8GieRu")}

and throws the below error

并抛出以下错误

/node_modules/mongoose/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:35
    throw new Error("Argument passed in must be a single String of 12 bytes or
          ^
Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at new ObjectID (/node_modules/mongoose/node_modules/mongodb/node_modules/bson/lib/bson/objectid.js:35:11)

  [1]: http://mongodb.github.io/node-mongodb-native/api-bson-generated/objectid.html

Mongoose is strictly checking the ObjectId of fixed length, how can i pass Object_id using mongoose with the given length

Mongoose 正在严格检查固定长度的 ObjectId,如何使用具有给定长度的 mongoose 传递 Object_id

回答by Markus W Mahlberg

You mix two concepts here.

你在这里混合了两个概念。

While "_id" can have any value (even subdocument like {firstName:'Foo',lastName:'Simpson'}, "ObjectId" has a fixed set of types with some restrictions, as the error message correctly states.

虽然“_id”可以有任何值(即使是像 一样的子文档{firstName:'Foo',lastName:'Simpson'},“ObjectId”有一组固定的类型,但有一些限制,正如错误消息正确指出的那样。

So your statement should be

所以你的陈述应该是

{'_id':'putWhatEverYouWantHere'}

回答by mrks

I had the problem in my router order:

我的路由器订单有问题:

app.get('/jobs', controllers.jobs.getAllJobs);
app.get('/jobs/karriere', controllers.jobs.getAllJobsXML);

app.get('/jobs/:id', controllers.jobs.getJob);
app.get('/jobs/:id/xml', controllers.jobs.getJobXML);

I defined /jobs/karriere after/jobs/:id so the application thought that "karriere" was an ObjectID and returned the error. The code above is the working one.

/jobs/:id之后定义了 /jobs/karriere 因此应用程序认为“karriere”是一个 ObjectID 并返回错误。上面的代码是有效的。

回答by Tashfeen

Make sure the method you are using in client and server side match. This error also shows when you have e.g. GETbeing sent from client side and POSTrequired on the server side.

确保您在客户端和服务器端使用的方法匹配。当您GET从客户端发送并POST在服务器端需要时,此错误也会显示。

回答by nagender pratap chauhan

same problem faced by me but after a RND . I identified that i passed wrong {Id:Undefined} so problem occured so please firstly check your Id which you passed in URL.

我面临同样的问题,但在 RND 之后。我发现我传递了错误的 {Id:Undefined} 所以问题发生了所以请首先检查你在 URL 中传递的 ID。

      Error = "http://localhost:4000/api/deleteBook/Undefined"
      Right = "http://localhost:4000/api/deleteBook/5bb9e79df82c0151fc0cd5c8"

回答by sibaspage

You are passing any

你正在通过任何

ObjectID undefinded 

If the ObjectID is undfined thaen this error will come.

如果未定义 ObjectID,则会出现此错误。

回答by Vince Banzon

In my case, I'm using mongoose. and I am not able to query with something like this:

就我而言,我使用的是猫鼬。我不能用这样的东西查询:

{ "_id" : "8GieRu" }

Until I bumped into my model file and specified this line counter.model.js

直到我碰到我的模型文件并指定了这一行 counter.model.js

var CounterSchema = new Schema({
  _id: String,
  sequence_value: Number
})

Notice that I specified the data type as string for _id in my model. and the, in my query, I didn't need to convert string to ObjectId.

请注意,我在模型中将数据类型指定为 _id 的字符串。而且,在我的查询中,我不需要将字符串转换为 ObjectId。

Query now works as simple as what the filter:

查询现在和过滤器一样简单:

{ "_id" : "8GieRu" }