我需要纠正这个错误:BSONTypeError:传入的参数必须是12字节的字符串或24个十六进制字符或整数的字符串.&l



服务端代码node.js, express app

app.put('/items/:id', async(req, res) => {
const id = req.params.id;
console.log(id, 'came from id=> app.put');
const qty = req.body;
console.log(qty, 'came from quantity=> app.put');
const filter = {
_id: ObjectId(id)
};
console.log(filter, 'came from filter=> app.put')
const options = {
upsert: true
};
console.log(options, 'came from options=> app.put')
const updatedDoc = {
$set: {
qty: qty,
}
}
console.log(updatedDoc, 'came from updateddoc=> app.put')
const result = await userItems.updateOne(filter, updatedDoc, options);
console.log(result, 'came from result=> app.put')
res.send(result);
})

客户端代码

const handleQuantity = event => {
event.preventDefault();
const qty = event.target.qty.value;
console.log(qty);
const url = `http://localhost:4000/items/${id}`
fetch(url, {
method: 'PUT',
body: JSON.stringify({
qty: qty
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then((response) => response.json())
.then(data => {
console.log(data);
});
}

我得到的错误:

port 4000 [object object] from id=>App.put {qty: '5'}来了从数量=比;app.put D: web开发 warehouse-management-server-side-pippal5536 node_modules bson lib error.js: 41Var _this = _super。Call (this, message) || this;^

BSONTypeError:传入的参数必须是一个长度为12字节的字符串字符串的24个十六进制字符或整数
在新的BSONTypeError (D:web开发仓库管理-server-side-pippal5536node_modulesbsonliberror.js:41:28)(D:web developmentwarehouse-management-server-side-pippal5536node_modulesbsonlib ObjectId .js:66:23)(D:web developmentwarehouse-management-server-side-pippal5536node_modulesbsonlib ObjectId .js:26:20)at D:web developmentwarehouse-management-server-side-pippal5536index.js:73:27在层。(D:web developmentwarehouse-management-server-side-pippal5536node_modulesexpresslibrouterlayer.js:95:5)web developmentwarehouse-management-server-side-pippal5536node_modulesexpresslibrouterroute.js:144:13在路线。调度(D:web developmentwarehouse-management-server-side-pippal5536node_modulesexpresslibrouterroute.js:114:3)在层。(D:web developmentwarehouse-management-server-side-pippal5536node_modulesexpresslibrouterlayer.js:95:5). at D:web developmentwarehouse-management-server-side-pippal5536node_modulesexpresslibrouterindex.js:284:15(D:web developmentwarehouse-management-server-side-pippal5536node_modulesexpresslibrouterindex.js:365:14)[nodemon]应用程序崩溃-在启动前等待文件更改…

如果您正在寻找导致错误的原因,那是因为传递给ObjectID()id无效。

相关内容

  • 没有找到相关文章

最新更新