node.js - findOneAndUpdate 异步等待不返回新文档
问题描述
下面的代码正在更新数据库,但没有返回新文档,尽管特别声明“returnNewDocument”为真。我没有使用猫鼬,而是使用了 MongoDB 驱动程序,如下所示https://docs.mongodb.com/manual/reference/method/db.collection.findOneAndUpdate/
const { MongoClient } = require("mongodb");
...
const mongo = new MongoClient(process.env.MONGODB_CONNECTION_URI, {
useUnifiedTopology: true
});
if (mongo) {
mongo.connect();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "build")));
app.listen(8080, () => console.log("Server running on 8080"));
}
app.get("/v1/editprofile", function(req, res) {
const { givenName, familyName, mobile, address, serviceRadius, language } = req.query;
if (req.cookies && req.cookies.participant) {
jwt.verify(req.cookies.participant, privateKey, { algorithms: ["RS256"] }, function(err, decoded) {
if (err) res.status(400).send({ status: "logout", description: "Bad Cookie found" });
async function insertProfile() {
const id = decoded.id;
const data = await mongo
.db("Users")
.collection("participants")
.findOneAndUpdate(
{ id },
{
$set: {
givenName,
familyName,
mobile,
address,
serviceRadius,
language
}
},
{
upsert: true,
returnNewDocument: true
}
);
if (data) {
const participant = data.value
console.log(participant)
res.status(200).send({ participant });
}
}
insertProfile().catch(console.error);
});
} else {
res.status(400).send({ status: "logout", description: "No Cookie found" });
}
});
解决方案
推荐阅读
- java - Java:如何为项目编码以继承原材料的属性?
- ssl - 尽管服务器配置了 ssl-certificate,但在点击 tokbox api 时出现 Guzzle 异常
- javascript - 如何更改javascript对象的键名和值
- flutter - 像引导程序一样在 Flutter 中构建布局
- html - 如何偏移一些表格单元格
- docker - 无法从 kubernetes 中的公共 ip 访问服务
- java - 返回状态消息
- node.js - 如何更改我的字段以保存对象数组?
- css - 用css变量减轻mixin不起作用
- laravel - Homestead 9 新盒子创建一个新的 Laravel 项目作曲家无法扫描 /vendor/phpunit/php-code-coverage/src/ 中的类