javascript - 如何附加nodejs sequelize json字段的模型列表
问题描述
如何附加在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}}
);
这该怎么做?
解决方案
您可以执行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
}
}
);
推荐阅读
- vue.js - CORS 在使用 LUMEN API 的 VUE webapp 中给出相同的策略错误
- php - 尽管政策允许我的站点引荐来源网址,但 S3 存储桶上的 copyObject 访问被拒绝
- javascript - 如何设置点以下的所有值( TileMap 像数组)
- wordpress - 如何设置登录页面以在 wordpress 中查看网站?
- html - 如何用中间的单词填空?
- java - 为什么我的 StringUtils 类没有 isBlank()
- html - 如何定位基于 *ngIf 条件加载的模板中的元素
- python - PyTorch 1.0 在 Python3.7 中加载 VGGFace2 权重
- javascript - 自动加载全屏模式
- firebase - 以高效的方式在 Firestore 中存储和查询帖子