简体   繁体   English

在nodejs中转换为对象id时抛出错误

[英]throwing error while converting to object id in nodejs

I am working on a task where i am taking the id generated from the database collection and i passing it through the postman.. i am converting that it to database obejct id created and its working accordinlg if we pass correct id else it is throwing an error 我正在处理一个任务,我正在从数据库集合中生成id并将其传递给邮递员。我正在将它转换为数据库obejct id创建并且如果我们传递正确的id,它的工作accinlg其他它正在抛出一个错误

mongo.get().collection("post").find({"_id":new ObjectId(req.headers.postid)}).toArray(function(err, result) {
       if (err) throw err;

         if(result.length==0){              
            jsonObj.response="post id entered is invalid ";
            res.send(jsonObj)
        }
        else{
        //some operations
        }
    }

when ever i am not passing the valid input it is throwing the following error 当我没有传递有效输入时,它会抛出以下错误

   <body>
        <h1>Argument passed in must be a single String of 12 bytes or a string of 24 hex characters</h1>
        <h2></h2>
        <pre>Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
    at new ObjectID (/home/vamsi/nodejs-training/myfirstproject/facebook/node_modules/bson/lib/bson/objectid.js:51:11)
    at Object.handle (/home/vamsi/nodejs-training/myfirstproject/facebook/routes/updatepost.js:36:47)
    at next_layer (/home/vamsi/nodejs-training/myfirstproject/facebook/node_modules/express/lib/router/route.js:103:13)
    at Route.dispatch (/home/vamsi/nodejs-training/myfirstproject/facebook/node_modules/express/lib/router/route.js:107:5)
    at /home/vamsi/nodejs-training/myfirstproject/facebook/node_modules/express/lib/router/index.js:195:24
    at Function.proto.process_params (/home/vamsi/nodejs-training/myfirstproject/facebook/node_modules/express/lib/router/index.js:251:12)
    at next (/home/vamsi/nodejs-training/myfirstproject/facebook/node_modules/express/lib/router/index.js:189:19)
    at Function.proto.handle (/home/vamsi/nodejs-training/myfirstproject/facebook/node_modules/express/lib/router/index.js:234:5)
    at Layer.router (/home/vamsi/nodejs-training/myfirstproject/facebook/node_modules/express/lib/router/index.js:23:12)
    at trim_prefix (/home/vamsi/nodejs-training/myfirstproject/facebook/node_modules/express/lib/router/index.js:226:17)</pre>
    </body>
</html>

1- 1-

If you're using mongoose, and you're trying to perform a search operation on the basis if _id you don't actually need to convert the passed id to objectId . 如果您正在使用mongoose,并且您尝试在_id的基础上执行搜索操作,则实际上您不需要将传递的id转换为objectId just use 只是用

 mongo.get().collection("post")find({"_id" : req.headers.postid })

This won't impose the passed id to be a constructable to monogdb's objectId as objectId in mongo db follows special pattern. 这不会强加传递的id是一个施工的,以monogdb的ObjectID为objectId在蒙戈分贝以下特殊模式。 Like 喜欢

12-byte structure, the first 4 bytes of the ObjectId represent the time in seconds since the UNIX epoch. 12字节结构,ObjectId的前4个字节表示自UNIX纪元以来的秒数。

The next 3 bytes of the ObjectId represent the machine identifier. ObjectId的接下来的3个字节表示机器标识符。

The next 2 bytes of the ObjectId represent the process ID. ObjectId的下两个字节表示进程ID。

And the last 3 bytes of the ObjectId represent a random counter value. 并且ObjectId的最后3个字节表示随机计数器值。

And if your passed id is not constructable to the pattern mentioned above this is definitely gonna throw the errors like you're getting 如果你传递的id不能构造成上面描述的模式,那肯定会抛出你正在获得的错误

2 2

Well I am not very sure about this approach but you can first check if id passed is OK or not with something like 嗯,我不太确定这种方法,但你可以先检查id传递是否正常

var id = null;
try {
 id  =  new ObjectId(req.headers.postid)
  mongo.get().collection("post").find({"_id":id}).toArray(function(err, result) {
....

}
 catch(error) {
 console.error(error);
 //there must be error with the id
} 
if(!validator.isMongoId(req.headers.postid)){
 res.send("invalid post id ")
}
else{
 mongo.get().collection("post").find({"_id":new ObjectId(req.headers.postid)}).toArray(function(err, result) {
       if (err) throw err;

         if(result.length==0){              
            jsonObj.response="post id entered is invalid ";
            res.send(jsonObj)
        }
        else{
        //some operations
        }
    }
}

before that install validator npm module 在那之前安装验证器npm模块

如果你对和尚没事,你可以使用它。
var Id = require('monk').id(req.headers.postid);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM