【问题标题】:throwing error while converting to object id in nodejs在nodejs中转换为对象ID时抛出错误
【发布时间】:2018-08-31 12:27:39
【问题描述】:

我正在处理一项任务,我正在获取从数据库集合生成的 id,并将其传递给邮递员。如果我们传递正确的 id,我将其转换为创建的数据库对象 id 及其工作的accordinlg正在抛出错误

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
        }
    }

当我没有传递有效输入时,它会引发以下错误

   <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>

【问题讨论】:

  • 您将非法值传递给new ObjectId,因此引发异常。你希望发生什么?
  • 我与数据库中的 id 值匹配,它正在相应地工作,但是在负面测试用例中,当输入无效 id 时,它不应该从数据库中获取任何记录,而是抛出一个错误..但是如果我们以相同模式的形式传递一个 id 即使不存在它也可以正常工作

标签: javascript node.js mongodb mongodb-query postman


【解决方案1】:

1-

如果您使用的是猫鼬,并且您尝试在_id 的基础上执行搜索操作,您实际上不需要将传递的id 转换为objectId。只需使用

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

这不会将传递的id 强加为monogdb 的objectId 的可构造,因为mongo db 中的objectId 遵循特殊模式。喜欢

12 字节结构,ObjectId 的前 4 个字节表示自 UNIX 纪元以来的时间,以秒为单位。

ObjectId 的后 3 个字节代表机器标识符。

ObjectId 的后 2 个字节表示进程 ID。

ObjectId 的最后 3 个字节代表一个随机计数器值。

如果你传递的 id 不能按照上面提到的模式构造,这肯定会像你得到的那样抛出错误

2

好吧,我不太确定这种方法,但您可以先检查 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
} 

【讨论】:

  • 如果我们使用它,它不会获取记录
  • 哦,对不起。我没有注意到,您的问题中没有mongoose 标签。这将适用于猫鼬。让我更新
  • 如果我们在 if 代码中使用 isMongoId(str) 验证 id 并且在其他情况下我们可以执行剩余的任务......它对我有用.." isMongoId(str) ) 是一个验证器 npm 模块函数 "
  • 太棒了。玩得开心
【解决方案2】:
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
        }
    }
}

在此之前安装验证器 npm 模块

【讨论】:

    【解决方案3】:

    如果和尚没问题,可以用这个。
    var Id = require('monk').id(req.headers.postid);

    【讨论】:

    • 感谢您的回复,我不知道,但如果没有任何效果我会尝试
    猜你喜欢
    • 1970-01-01
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    相关资源
    最近更新 更多