gpt4 book ai didi

typescript - findOneAndUpdate 不更新

转载 作者:行者123 更新时间:2023-12-02 03:24:10 27 4
gpt4 key购买 nike

我正在尝试使用 findOneAndUpdate 更新存储在 mongoose 服务器中的文档之一。我将其设置为 PUT 请求,这就是我定义它的方式。

public updateConfigWithType(req: Request, res: Response) {
configs.findOneAndUpdate({'companyInfo.uniquecompanyid': req.params.id}, { $set: { companyName: "ITSWORKING" }} ,(err,doc) => {
if (err) {
res.send(err);
}else {
res.json({
message: 'Succesfully updated this item'
});
}
})
}

我的 PUT 请求 URL 设置如下:http://localhost:3000/parserConfig/123123wfdwedfwfwefwef

这是我的 mongoose 服务器文档中存在的一小部分 JSON 数据:

{
"_id": {
"$oid": "5bbf27ad4cf8a6be65ea6257"
},
"companyInfo": {
"companyName": "example",
"hashKey": "sdfsdf",
"hashFunction": "sdfsdf",
"uniquecompanyid": "123123wfdwedfwfwefwef"
},
"version": "1",
"__v": 0,
"parserConfig": {
"id": {
"fieldName": "key",
"path": "key"
},

因此,我希望能够通过 URL 中的 uniquecompnayid 找到正确的文档,并且我特别希望使用 req.body< 中存在的内容更新 parserConfig 字段。为了测试目的,我现在对所有内容进行了硬编码。发送 PUT 请求目前在 Postman 中给出了这个答案:

{"message":"Succesfully updated this item"}

但没有任何更新。我错过了什么?

更新

我出于测试目的更改了代码...这就是我尝试过的:

public updateConfigWithType(req: Request, res: Response) {
var query = {'companyInfo.uniquecompanyid':req.params.id};
configs.findOneAndUpdate(query, {$set:{'companyInfo.uniquecompanyid':"heheheheh"}}, {new:true}, function(err, doc){
if (err) {
return res.send(err);
}
return res.send(doc);
});
}

postman 中的响应是旧文档。没有任何改变

spyros 请求后更新

10:26:34 PM web.1 |  Mongoose: configs.findOne({ 'companyInfo.uniquecompanyid': '123123wfdwedfwfwefwef' }, { new: true, projection: {} })
10:26:34 PM web.1 | PUT /parserConfig/123123wfdwedfwfwefwef 200 158.118 ms - 2089

更新新信息通知

我发现了这个:https://github.com/Automattic/mongoose/issues/7011

它说:

我注意到,当使用不在架构中的更新文档属性调用 findOneAndUpdate 时,调试输出显示(错误)?采集方式。

为什么可以通过 Studio 3t 更新相同的属性,但不能通过我的代码更新?

最佳答案

启用调试日志记录:mongoose.set('调试', true);。这将显示执行的查询。我认为该错误可能与某些属性或不同对象 ID 的拼写错误有关。如果这没有帮助,请使用调试结果信息更新您的问题,以便我可以更新我的答案。

关于typescript - findOneAndUpdate 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53870647/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com