我正在使用快递。在从Api发送响应之前,我在MongoDb中有文件路径。我正在使用文件路径之前的api Url
song.path = url + song.path;
res.send(song);
但这样做是为了更新mongodb中的路径,即使我没有保存这个文档,我只是发送响应
我试过了
const newSong = song;
newSong.path = url + newSong.path;
res.send(newSong)
;
但这没有帮助,文件仍在更新。
这是我的控制器
const song = await Song.findOne({ 'lastPlayed': false }).sort("-totalRating");
song.lastPlayed = true;
song.save();
song.path = url + song.path;
res.send(song);
它应该只更新响应的属性..不在Db中
UPDATE
如果我评论出来qazxsw poi路径没有得到更新。
发生的事情是song.save()
是异步的,当你改变对象并发送响应时doc.save()
只在主线程之后执行,这就是doc.save()
复制到mongoDB的原因,即使你认为你以前保存过它。
您可以只为响应创建一个新对象
song.path = url + song.path;
或者你可以const song = await Song.findOne({ 'lastPlayed': false }).sort("-totalRating");
song.lastPlayed = true;
song.save();
res.send({ ...song, path: url + song.path });
保存,然后改变对象的响应,如果你想在发送响应之前处理错误,这是好的。
await