续集“findbyid";不是一个函数,但显然是“findAll".是 [英] sequelize "findbyid" is not a function but apparently "findAll" is

查看:11
本文介绍了续集“findbyid";不是一个函数,但显然是“findAll".是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 sequelize 时遇到了一个非常奇怪的问题,当我尝试调用函数 findAll 时它工作正常(创建和销毁相同),但是当我尝试调用函数findById"时,它会抛出findById is not a函数"(与FindOne"相同).

I am getting a very strange problem with sequelize, When I try to call the function findAll it works fine (same for create and destroy), but when I try to call function "findById", it throws "findById is not a function" (same for "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");
        })
    });

Gamme.js 模型

Gamme.js model

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;
};

推荐答案

在 Sequelize v5 中,findById() 被 findByPk() 取代.使用 findByPk 替换 findById ,一切正常.您可以在这里

With Sequelize v5, findById() was replaced by findByPk(). Replace findById using findByPk and everything should work fine. You can find query doc here

这篇关于续集“findbyid";不是一个函数,但显然是“findAll".是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆