Sequelize destroy方法返回NaN而不是删除的行数

时间:2017-05-10 00:11:39

标签: node.js sequelize.js

我正在尝试使用如下所示的destroy实例方法,但是我一直在接收NaN而不是删除的行数。

const Sequelize = require('sequelize');
const sequelize = new Sequelize(options);

const user = sequelize.define('user', {
    firstName: {
        type: Sequelize.STRING,
        field: 'first_name' // Will result in an attribute that is firstName when user facing but first_name in the database
    },
    lastName: {
        type: Sequelize.STRING
    }
}

// insert some records with user object, etc

user.destroy({ truncate: true }).asCallback((err, deleted) => {

    if (err) {
        throw err;
    }

    console.log(deleted);
    // Always prints NaN
});

任何帮助将不胜感激!

由于

1 个答案:

答案 0 :(得分:-1)

截断模型做类似的事情

  var model = require('../models');
  model.user.destroy({ truncate : true, cascade: false }).then(function(result)
        {
            res.json(result)
        }).catch(function(err){
            res.json(err)
        })

或者如果要删除特定记录,请执行以下操作。如果不受影响,这将返回effected0的行数

        user.destroy({
            where: {
                email: "test@gmail.com"
            }
        })
            .then(function (delete_rows) {
                console.log(delete_rows);
                res.json(delete_rows)
            })
            .catch(function (error){
                res.status(500).json(error);
            });

请参阅更新的文档[http://docs.sequelizejs.com/en/latest/docs/querying/]

http://docs.sequelizejs.com/en/v3/api/model/#destroyoptions-promiseinteger 2