将"findbyid"序列化不是函数,而是显然是"findAll".是 [英] sequelize "findbyid" is not a function but apparently "findAll" is

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

问题描述

我在续集方面遇到了一个非常奇怪的问题,当我尝试调用函数findAll时,它运行良好(与创建和销毁相同),但是当我尝试调用函数"findById"时,它抛出"findById不是功能"(与"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.

这篇关于将"findbyid"序列化不是函数,而是显然是"findAll".是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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