亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Sequelize.js 返回關聯表的舊值

Sequelize.js 返回關聯表的舊值

大話西游666 2024-01-18 15:46:31
我正在嘗試更新產品的狀態(產品表)。每個產品都有一個statusId,為“1”或“2”。每個產品的 statusId 的默認值為“1”,如果訪問一次路由,則應更改為“2”(反之亦然)。“statusId”和“status”{“id”}應始終相同。當我訪問路由時,數據庫中的“statusId”狀態發生更改,但“status”的 HTTP 響應與數據庫中的實際值不同。響應示例:{    "id": 1,    "name": "Drone",    "barcode": "123456789",    "description": "A drone that can fly",    "image": "drone.jpg",    "createdAt": "2020-12-22T17:30:15.000Z",    "updatedAt": "2020-12-22T17:30:22.841Z",    "statusId": 2,    "status": {        "id": 1,        "name": "in",        "createdAt": "2020-12-22T17:30:15.000Z",        "updatedAt": "2020-12-22T17:30:15.000Z"    }}協會:db.status.hasMany(db.product, {    foreignKey: {allowNull: false} ,});db.product.belongsTo(db.status, {    foreignKey: {allowNull: false}});路線: app        .route('/api/product/status/:id')        .put([authJwt.verifyToken], controller.updateProductStatus);控制器:exports.updateProductStatus =  (req, res) => {    // Get product with id and update status only     Product.findOne({        where: {            id: req.params.id        },        include: [            Status        ]    })        .then(product => {            let newStatus = product.statusId === 1 ? 2 : 1;            product.update({                statusId: newStatus            })                .then(                    res.status(200).send(product)                )                .catch(err => {                    console.log("Error (Update product): " + err);                    res.status(404).send(err);                })        })        .catch(err => {            console.log("Error (find old product): " + err);            res.status(404).send(err);        });};如何更改代碼以返回“status”的“id”的正確值?
查看完整描述

1 回答

?
慕尼黑8549860

TA貢獻1818條經驗 獲得超11個贊

您應該像這樣重新加載模型實例:


.then(

  product.reload({ include: [Status]})

    .then(res.status(200).send(product))

 )


查看完整回答
反對 回復 2024-01-18
  • 1 回答
  • 0 關注
  • 188 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號