Why .save( ) is not and throwing error as ``TypeError: sourceData.save is not a function``?
Why .save( ) is not and throwing error as ``TypeError: sourceData.save is not a function``?
这是我的源模型的代码:
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
值并保存它。
articles 是一个对象数组 [{....},{.....},{.....}]
这是我的更新代码:
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.save()
之前使用 sourceData.markModified('currentState')
,但后来它开始提供 TypeError: sourceData.markModified is not a function
。
我什至试过 sourceData.update()
但还是不行。
我是 node.js 的新手,我遵循的教程仅使用 .save() 来更新数据,我在那里工作。请帮我解决这个问题。
.find
方法returns一个数组作为结果。您可能想改用 .findOne
。
这是我的源模型的代码:
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
值并保存它。
articles 是一个对象数组 [{....},{.....},{.....}]
这是我的更新代码:
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.save()
之前使用 sourceData.markModified('currentState')
,但后来它开始提供 TypeError: sourceData.markModified is not a function
。
我什至试过 sourceData.update()
但还是不行。
我是 node.js 的新手,我遵循的教程仅使用 .save() 来更新数据,我在那里工作。请帮我解决这个问题。
.find
方法returns一个数组作为结果。您可能想改用 .findOne
。