這是我的源模型的代碼:
const sourceSchema = new Schema(
{
name:{
type: String,
required: true
},
url:{
type: String,
required: true
},
data:{
posts:[
{
postId: {
type: Schema.Types.ObjectId,
ref: 'Post',
required: true
}
}
]
},
oldState:{
type: Array,
},
currentState:{
type: Array
}
}
);
我在資料庫中保存了一個源,現在我想更新currentState值并保存它。文章是一個物件陣列[{....},{.....},{.....}]
這是我的更新代碼:
Source.find({name: 'National News'})
.then(sourceData=>{
console.log(sourceData);
sourceData.currentState= articles;
return sourceData.save();
})
.then(result=>{
console.log(result);
next();
})
.catch(err=>{
console.log(err);
})
我能夠在控制臺中看到已選擇正確的源以console.log(sourceData)提供正確的資料,但在那之后,我得到錯誤TypeError: sourceData.save is not a function
我之前也嘗試過使用sourceData.markModified('currentState'),sourceData.save()但后來它開始給TypeError: sourceData.markModified is not a function.
我什至嘗試過sourceData.update(),但這也沒有用。
我是 node.js 的新手,我遵循的教程僅使用 .save() 來更新資料,我在那里作業。請幫我解決這個問題。
uj5u.com熱心網友回復:
該.find方法回傳一個陣列作為結果。您可能想.findOne改用。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/433709.html
