【问题标题】:How to append nodejs sequelize model list of json field如何附加nodejs sequelize json字段的模型列表
【发布时间】:2020-05-13 02:43:30
【问题描述】:

如何在nodejs Sequelize中追加JSON列表

数据库:Postgres

我有一个这样的模型字段:

student_list:
    {
        type: Sequelize.ARRAY(Sequelize.JSON),
        allowNull: true
    },

我正在尝试以这种方式附加 JSON 的 Sequelize Array 但未成功附加列表:

var data =  ({'share_by':"aaa",'list_name':"list_name"})
student.update( 
                {'student_list': Sequelize.fn('array_append', Sequelize.col('student_list'), data)},
                {'where': {studet_id: student_id}}
                );

如何做到这一点?

【问题讨论】:

    标签: javascript node.js json postgresql sequelize.js


    【解决方案1】:

    您可以执行find() 并获取元素值、更新元素并使用更新的数据进行更新。像这样:

    // Get the actual student data
    const student = await student.findOne(studentId);
    
    // Spread and add the new value
    const student_list = {
       ...student_list,
       data
    };
    
    // Update the field
    await student.update(
        {
            student_list
        },
        {
            where: {
                student_id
            }
        }
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-20
      • 2022-09-25
      • 1970-01-01
      • 2020-03-27
      相关资源
      最近更新 更多