如何使用sequelize在nodejs中更新?

时间:2019-03-16 07:47:43

标签: mysql node.js sequelize.js

在这里,当我将金额存入钱包时,我想更新钱包表 我的代码是这样的  模型wallet.js

'use strict';
module.exports = (sequelize, DataTypes) => {
    var Model = sequelize.define('wallet', {
        id: {
            type: DataTypes.INTEGER,
            field: 'id',
            primaryKey: true,
            autoIncrement: true
        },
        cid: {
            type: DataTypes.STRING,
            field: 'cid'
        },
        deposit: {
            type: DataTypes.INTEGER,
            field: 'deposit'
        },
        withdrawal: {
            type: DataTypes.INTEGER,
            field: 'withdrawal'
        },
        available: {
            type: DataTypes.INTEGER,
            field: 'available',
        },
        comments: {
            type: DataTypes.DATE,
            field: 'comments'
        },
        online: {
            type: DataTypes.STRING,
            field: 'online'
        }
    }, {
        tableName: 'wallet',
        timestamps: false
    });
    Model.associate = function (models) {
        this.orders = this.hasOne(models.orders, {
            as: 'orders',
            foreignKey: 'wid'
        });
    };

    Model.prototype.toWeb = function (pw) {
        let json = this.toJSON();
        return json;
    };
    return Model;
};

这里发生了钱包存款,我正在使用set方法更新钱包

   walletcontroller.js

const deposit = async function (req, res) {
    res.setHeader('Content-Type', 'application/json');
    let err, wallet, existingWallet;
    let wallet_info = req.body;
    [err, existingWallet] = await to(Wallet.findAll({
        limit: 1,
        where: {
            cid: wallet_info.cid,
        },
        order: [
            ['id', 'DESC']
        ]
    }));
    [err, wallet] = await to(Wallet.set(wallet_info, {
        limit: 1,
        where: {
            cid: wallet_info.cid,
        },
        order: [
            ['id', 'DESC']
        ]
    }));
    if (err) return ReE(res, err, 422);
    if (existingWallet[0] != 'undefined') {
        wallet.available = existingWallet[0].available + wallet_info.deposit;
        wallet.comments = new Date();
    } else {
        wallet.available = wallet_info.deposit;
        wallet.comments = new Date();
    }
    console.log("avalible balance:" + wallet.available);
    [err, wallet] = await to(wallet.save());
    if (err) return ReE(res, err, 422);
    return ReS(res, {
        wallet: wallet.toWeb()
    }, 201);
}
module.exports.deposit = deposit;

请帮我了解如何更新钱包...当我在这里用callig api时,我的错误信息看起来像这样

     Executing (default): SELECT `id`, `cid`, `deposit`, `withdrawal`, `available`, `comments`, `online` FROM `wallet` AS `wallet` WHERE `wallet`.`cid` = '33' ORDER BY `wallet`.`id` DESC LIMIT 1;

错误:

Uncaught Error { filename: '\\cl-api-master\\controllers\\WalletController.js',
  line: 67,
  row: 37,
  message: 'Wallet.set is not a function',
  type: 'TypeError',
  stack: 'TypeError: Wallet.set is not a function\n    at deposit (E:\\cl-api-master\\controllers\\WalletController.js:67:37)\n    at <anonymous>',
  arguments: undefined }
POST /v1/wallet/deposit - - ms - -

1 个答案:

答案 0 :(得分:0)

对于Edition,您只需使用-

routes
可以在更新查询上方运行if else条件来设置

可用变量。