微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用sequelize / postgres添加嵌套的JSONB字段

如何解决如何使用sequelize / postgres添加嵌套的JSONB字段

User模型中,有一个JSONB字段viewer_data,例如,null当前是user。需要在jwt_secret添加一个值为1的新字段viewer_data。在jwt_secret添加viewer_data后,其外观应如下所示:

  user : {viewer_data : {"jwt_token":1}}

尝试了setinsert,但它们都不起作用:

   user = await User.findByPk(id);
   console.log(user.viewer_data); //<<==output is null 
   //
   user.set("viewer_data.jwt_secret",1); //<<==throw an error of can't convert null to object as viewer_data is null
   user.insert("view_data.jwt_secret",1); //<<==error as insert is not a function

postgres版本是11,后继版本是6.3。

解决方法

当JSONB字段viewer_data为空时。更新应这样进行:

  user.set("viewer_data",{"prev_jwt_secret": xxx,"jwt_secret": xxx}};

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。