MongoDB, BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
MongoDB, BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
我正在做我的项目,没有任何错误,但是服务器端突然崩溃,错误是“BSONTypeError:传入的参数必须是一个 12 字节的字符串或一个 24 个十六进制字符的字符串或一个整数”
我已经从 Whosebug 尝试了针对这个类似问题的其他建议,但它不起作用。
我附上了-
const ObjectId = require('mongodb').ObjectId;
还没有解决。代码完整错误为
var _this = _super.call(this, message) || this;
^
BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
at new BSONTypeError (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\bson\lib\error.js:41:28)
at new ObjectId (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\bson\lib\objectid.js:66:23)
at ObjectId (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\bson\lib\objectid.js:26:20)
at F:\Web Development\Projects\Fiverr\hridayshaha\server\index.js:107:24
at Layer.handle [as handle_request] (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\layer.js:95:5)
at next (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\layer.js:95:5)
at F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\index.js:284:15
at param (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\index.js:365:14)
[nodemon] app crashed - waiting for file changes before starting...
index.js:107:24 --- console.log(id);
来自第 105 行
app.get('/events/:id', async(req, res) =>{
const id = req.params.id;
console.log(id);
const query = {_id: ObjectId(id)};
const items = await eventsCollection.findOne(query);
res.json(items);
您的代码中没有输入验证,但您不能假设 req.params.id
始终是有效的 ObjectId
。
您可以修改路由路径以确保路由处理程序只接受看起来像有效的内容 ObjectId
:
app.get('/events/:id([0-9a-fA-F]{24})', async (req, res) => { … });
已记录 here。
我正在做我的项目,没有任何错误,但是服务器端突然崩溃,错误是“BSONTypeError:传入的参数必须是一个 12 字节的字符串或一个 24 个十六进制字符的字符串或一个整数”
我已经从 Whosebug 尝试了针对这个类似问题的其他建议,但它不起作用。
我附上了- const ObjectId = require('mongodb').ObjectId;
还没有解决。代码完整错误为
var _this = _super.call(this, message) || this;
^
BSONTypeError: Argument passed in must be a string of 12 bytes or a string of 24 hex characters or an integer
at new BSONTypeError (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\bson\lib\error.js:41:28)
at new ObjectId (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\bson\lib\objectid.js:66:23)
at ObjectId (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\bson\lib\objectid.js:26:20)
at F:\Web Development\Projects\Fiverr\hridayshaha\server\index.js:107:24
at Layer.handle [as handle_request] (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\layer.js:95:5)
at next (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\route.js:144:13)
at Route.dispatch (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\route.js:114:3)
at Layer.handle [as handle_request] (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\layer.js:95:5)
at F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\index.js:284:15
at param (F:\Web Development\Projects\Fiverr\hridayshaha\server\node_modules\express\lib\router\index.js:365:14)
[nodemon] app crashed - waiting for file changes before starting...
index.js:107:24 --- console.log(id);
来自第 105 行
app.get('/events/:id', async(req, res) =>{
const id = req.params.id;
console.log(id);
const query = {_id: ObjectId(id)};
const items = await eventsCollection.findOne(query);
res.json(items);
您的代码中没有输入验证,但您不能假设 req.params.id
始终是有效的 ObjectId
。
您可以修改路由路径以确保路由处理程序只接受看起来像有效的内容 ObjectId
:
app.get('/events/:id([0-9a-fA-F]{24})', async (req, res) => { … });
已记录 here。