验证错误:使用JSONB保存时未定义数据

时间:2019-05-18 00:24:28

标签: json postgresql sequelize.js

Sqeualize(5.7.6)用于将包含JSONB字段的消息保存到postgres(11)。这是代码

let msg = Message.build(_.pick(req.body, ['sender_id' ,
                                           'event_id',
                                        ]));
msg.fort_token = req.user.fort_token;
msg.set("data.msg_body", req.body.data.msg_body);  //set used to assign JSON value
await msg.save()

错误是:

a new message Validation error: data is not defined

这是msg对象在保存之前的输出。 data字段(JSONB)有一个值

message {
  dataValues:
   { id: null,
     sender_id: 22,
     event_id: 1,
     fort_token: 'SI3FBmtQDtyodVTXkTsYMp7PziKxwIwX',
     data: { msg_body: 'hiii' } },  //<<<<== here is the data value
  _previousDataValues:
   { sender_id: undefined,
     event_id: undefined,
     fort_token: undefined },
  _changed:
   { sender_id: true, event_id: true, fort_token: true, data: true },
  _modelOptions:
   { timestamps: true,
     validate: { msg_n_content_info: [Function: msg_n_content_info] },
     freezeTableName: false,
     underscored: false,
     paranoid: false,
     rejectOnEmpty: false,
     whereCollection:
      { event_id: '1',
        fort_token: 'SI3FBmtQDtyodVTXkTsYMp7PziKxwIwX' },
     schema: null,
     schemaDelimiter: '',
     defaultScope: {},
     scopes: {},
     indexes: [ [Object], [Object], [Object], [Object] ],
     name: { plural: 'messages', singular: 'message' },
     omitNull: false,
     updatedAt: false,
     sequelize:
      Sequelize {
        options: [Object],
        config: [Object],
        dialect: [PostgresDialect],
        queryInterface: [QueryInterface],
        models: [Object],
        modelManager: [ModelManager],
        connectionManager: [ConnectionManager],
        importCache: {},
        test: [Object] },
     hooks: {} },
  _options: { isNewRecord: true, _schema: null, _schemaDelimiter: '' },
  isNewRecord: true }

这是data字段的模型定义:

data: {type: Sql.JSONB,
           allowNull: false

data中的msg字段对我来说很好。为什么保存时未定义?

0 个答案:

没有答案