I'm using Sequelize with Node and I have an instance of a model called athlete. I'm trying to insert it into my database using the save() function but it is returning this:
TypeError: athlete.save is not a function
Here is my code snippet :
console.log("Athlete: " + athlete);
athlete.save().then(function() {
console.log("Inserted Athlete");
callback(null, athlete);
}).catch(function (err) {
console.log("Inserted athlete with error: " + err);
callback(err, null);
});
The log prints this:
Athlete: [object SequelizeModel]
athlete is created from a class method in a Sequelize model definition file.
答案 0 :(得分:0)
原来SequelizeModel不是正确的对象类型。它必须是SequelizeInstance类型的对象才能使用save()函数保存。
答案 1 :(得分:0)
在保存之前尝试构建实例:
const athlete = Athlete.build(req.body,
{
fields: ['athlete']
})
.save()
.then( // . //)
.catch(err => {res.json(err)})