gpt4 book ai didi

node.js - Mongoose findOneAndUpdate 返回无效错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:33:24 26 4
gpt4 key购买 nike

我正在尝试向此方案添加评论:

var CommentSchema = new Schema(
{
ad_id:String,
comments:
[
{
author: String,
authorID: Number,
posted: Date,
text: String,
title: String
}
]
}

但是如果ad_id存在,我只想将新评论推送到comments,基本上创建一个upsert查询:

var query = {'ad_id': req.body.data.ad_ID}
var doc = {$set:{'ad_id':req.body.data.ad_ID},$push:{'comments':{'author':req.body.data.author,'authorID':req.body.data.uID,'posted':req.body.data.posted
,'text':req.body.data.text, 'title':req.body.data.title}}};
var options = {upsert:true};

Comments.findOneAndUpdate(query,doc,options, function (err, doc) {
if(err) {
console.log(String(err));
res.send({"foo": String(err)});
}
else {
console.log((doc));
res.send(doc);
}
});

但是在得到以下错误:

Unable to invalidate a subdocument that has not been added to an array.

最佳答案

事实证明我有数据类型错误导致了这个问题。如果有人遇到该错误代码,请确保数据类型与输入匹配。

关于node.js - Mongoose findOneAndUpdate 返回无效错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33413452/

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