Node.js 发送空值时,mongodb中的日期字段未得到更新
我开发了一个平均堆栈应用程序。我正在尝试使用日期字段的空值更新记录。这在本地主机上运行正常,但在服务器(Aws Ec2)上运行不正常。它包含更新之前的值 我也尝试过“未定义”,但仍然是同一个问题 拜访Node.js 发送空值时,mongodb中的日期字段未得到更新,node.js,angular,mongodb,mongoose,Node.js,Angular,Mongodb,Mongoose,我开发了一个平均堆栈应用程序。我正在尝试使用日期字段的空值更新记录。这在本地主机上运行正常,但在服务器(Aws Ec2)上运行不正常。它包含更新之前的值 我也尝试过“未定义”,但仍然是同一个问题 拜访 我希望它也能在我的服务器上运行。我也做过类似的事情,当用户使用Mongoose登录时,更新他们最后的登录日期 const authUser: any = await new Promise((res, rej) => { User.findOneAndUpdate({ _id: us
我希望它也能在我的服务器上运行。我也做过类似的事情,当用户使用Mongoose登录时,更新他们最后的登录日期
const authUser: any = await new Promise((res, rej) => {
User.findOneAndUpdate({ _id: user[0]._id }, { last_login: date }, {new: true}, (err: any, user:any) => {
if (err) rej(err);
res(user);
});
});
正如你所看到的,我把它包装在一个承诺中,这样我就可以等待回信了
通过查看您的,我希望您不会告诉它使用findbyiandupdate
查找,也不会使用findOneAndUpdate
查找,因为您只传递ID。因此更新后的应该是这样的
Assignment.findByIdAndUpdate(req.params.id, assignment, {new:true}, (err,doc)=>{
if(err) console.log('Error in Assignment Update: '+JSON.stringify(err,undefined,2));
return res.send(doc);
});
因此,在上面的一个例子中,我们传递的是findByIdAnyUpdate
的ID,我们只是传递赋值。如果它包含与模式相同的所有字段,它将使用最新字段进行更新
编辑:
您可以尝试的另一种方法是:
Assignment.findByIdAndUpdate(req.params.id, assignment, {new: true}, (err: any, result: any) => {
if(err) console.log('Error in Assignment Update: '+JSON.stringify(err,undefined,2));
// Reasign with new assignment and save
result = assignment;
result.save();
return res.send(result);
});
那么为什么它在我的本地主机上工作得很好呢?我也应用了您的解决方案,但相同的问题仍然存在于我的服务器上,而不是本地主机上。让我看看这是否发生在我的服务器上。您的解决方案也正确。我的服务器出现了一些奇怪的问题。它正在更新AppointTime,但不是countEndTime。两者都是相同的日期类型。看看我用mongooseCheck更新的另一种方法在你的机器和服务器上检查你的mongo版本
Assignment.findByIdAndUpdate(req.params.id, assignment, {new: true}, (err: any, result: any) => {
if(err) console.log('Error in Assignment Update: '+JSON.stringify(err,undefined,2));
// Reasign with new assignment and save
result = assignment;
result.save();
return res.send(result);
});