Ama*_*ine 22 orm node.js sequelize.js
我在sequelize中遇到一个非常奇怪的问题,当我尝试调用函数findAll时,它工作正常(同样适用于create和destroy),但是当我尝试调用函数"findById"时,它会抛出"findById不是函数"(同样适用于"FindOne").
//works fine
var gammes = models.gamme.findAll().then(function(gammes) {
res.render('admin/gammes/gestion_gamme',{
layout: 'admin/layouts/structure' ,
gammes : gammes,
js: "gammes"
});
});
// throws models.gamme.findById is not a function
models.gamme.findById(req.params.id).then(function(gamme) {
gamme.update({
nom: req.body.nom
}).then(function () {
res.redirect("/gammes");
})
});
Run Code Online (Sandbox Code Playgroud)
Gamme.js模型
module.exports = function (sequelize, DataTypes) {
"use strict";
var gamme = sequelize.define('gamme', {
id_gamme: {
type: DataTypes.INTEGER.UNSIGNED,
autoIncrement: true,
primaryKey: true
},
nom: {
type: DataTypes.STRING,
allowNull: false
}
}, {
classMethods: {},
timestamps: false
});
return gamme;
};
Run Code Online (Sandbox Code Playgroud)
小智 54
使用Sequelize v5,findById()被findByPk()取代.使用findByPk替换findById,一切都应该正常工作.
小智 10
sequelize 的团队正在删除这个函数并用一个新函数来代替它
按包查找
像这样
// search for known ids
Project.findByPk(123).then(project => {
// project will be an instance of Project and stores the content of the table entry
// with id 123. if such an entry is not defined you will get null
})
Run Code Online (Sandbox Code Playgroud)