javascript - sequelize 创建或更新关联

标签 javascript node.js sequelize.js

我在使用 sequelize 在我的数据库上添加数据时遇到问题。

我有 2 个表与关联 howmany:

const Price = sequelize.define('prices', {
      close: {
          type: Sequelize.INTEGER
      },
        open: {
            type: Sequelize.INTEGER
        },
        low: {
            type: Sequelize.INTEGER
        },
        high: {
            type: Sequelize.INTEGER
        },
      date: {
          type: Sequelize.DATE
      }
    });

const Crypto = sequelize.define('cryptos', {
      uuid: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV1,
        primaryKey: true
      },
      shortname: {
          type: Sequelize.STRING
      },
      name: {
          type: Sequelize.STRING
      },
        totalsupling:{
        type: Sequelize.STRING
        },
        imageurl:{
        type: Sequelize.STRING
        },
        prooftype:{
        type: Sequelize.STRING
        },
        premined:{
            type: Sequelize.STRING
        }
    });

db.cryptos.hasMany(db.price, {foreignKey: 'fk_cryptoid', sourceKey: 'uuid'});
db.price.belongsTo(db.cryptos, {foreignKey: 'fk_cryptoid', targetKey: 'uuid'});

我可以添加新条目:
Crypto.create({
    shortname: 'ETH',
    name: 'ethereum',
    totalsupling: 200000000,
    rank: 2,
    prices: [
        {
            close: '125',
                    open: '150',
                    high: '190',
                    low: '102'
            date: new Date()
        },
        {
            close: '125',
                    open: '150',
                    high: '190',
                    low: '102'
            date: new Date()
        }
    ]
}, {
    include: [ Price ]
}).then(() => {
    res.send("Done!");
})

但是我无法在已经存在的加密实体中添加关联的新实体价格:
Crypto.findOne({
    where: {uuid: 'f0a7e0e0-793d-11e8-b166-d7ecc2040fbe'},
    include: [ {model: Price }]
}).then(
    function (obj) {
       return obj.Price.updateAttributes(
                {
                    close: '125',
                    open: '150',
                    high: '190',
                    low: '102'
                }
                ).then(() => {
            console.log('done!!')
        })
    });

消息错误:
Unhandled rejection TypeError: Cannot read property 'updateAttributes' of undefined
at /home/blind/Documents/Init-Site-Info-Crypto/Controller/init.controller.js:65:29
at tryCatcher (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/util.js:16:23)
at Promise._settlePromiseFromHandler (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/promise.js:512:31)
at Promise._settlePromise (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/promise.js:569:18)
at Promise._settlePromise0 (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/promise.js:614:10)
at Promise._settlePromises (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/promise.js:693:18)
at Async._drainQueue (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/async.js:133:16)
at Async._drainQueues (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/async.js:143:10)
at Immediate.Async.drainQueues [as _onImmediate] (/home/blind/Documents/Init-Site-Info-Crypto/node_modules/bluebird/js/release/async.js:17:14)
at runCallback (timers.js:800:20)
at tryOnImmediate (timers.js:762:5)
at processImmediate [as _immediateCallback] (timers.js:733:5)

最佳答案

在您的协会中,加密货币具有 许多 价格。因此,如果您在加密中包含您的价格模型,它应该返回一个价格数组,并且您不能直接在该数组中执行 updateAttributes。这是一个问题。另一个问题是您的代码无法关联表。

尝试为您的关联使用别名。像这样的东西:

db.cryptos.hasMany(db.price, {as: 'prices', foreignKey: 'fk_cryptoid', sourceKey: 'uuid'}); 
db.price.belongsTo(db.cryptos, {as: 'crypto', foreignKey: 'fk_cryptoid', targetKey: 'uuid'});

然后你可以访问像
Crypto.findOne({
    where: {uuid: 'f0a7e0e0-793d-11e8-b166-d7ecc2040fbe'},
    include: [{model: Price, as: 'prices'}]
})
.then(function (prices) {
   console.log(prices)
})

关于javascript - sequelize 创建或更新关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51045087/

相关文章:

javascript - 在 Node.js 中与 Wit.ai 聊天机器人开始对话

mysql - 在 sequelize 中将关联的表属性作为主表属性传递

javascript - 当站点不是其 URL 的根时,如何在 getJSON 调用中定位 API

javascript - D3数据绑定(bind)(二维数组)

javascript - 显示变量中的数据

javascript - Sails.js + Grunt "Cannot find module"错误

javascript - 漏洞?范围选择器按钮无法正常工作

node.js - FFMPEG 仅执行一次 HTTP 范围请求 (HTML5 MediaStream/WebM)

node.js - 使用 Azure SQL DB(express/node)配置 Sequelize

node.js - Sequelize OP -> 类型错误 : Cannot read property 'like' of undefined