sequelize findOrCreate没有返回数组

时间:2017-10-24 17:05:50

标签: javascript node.js sequelize.js

坚持使用Sequelize findOrCreate方法。
所以我希望得到tags的空数组,但我得到tag.id, tag.name ...等等。

[ { id: 2,
    name: 'Inf 1',
    email: 'andrew@ww.rt',
    createdAt: 2017-10-24T16:30:38.314Z,
    updatedAt: 2017-10-24T16:31:02.076Z,
    category_id: null,
    'tags.id': null, <---- Here is should be just like tags: []` 
    'tags.name': null,
    'tags.description': null,
    'tags.createdAt': null,
    'tags.updatedAt': null,
    'tags.UsersTags.createdAt': null,
    'tags.UsersTags.updatedAt': null,
    'tags.UsersTags.user_id': null,
    'tags.UsersTags.tag_id': null },
  false ] 

模型用法

User.findOrCreate({
    raw: true,
    where: query,
    defaults: update,
    include: [{
      model: Model.Tag,
      as: 'tags'
    }]
  })

1 个答案:

答案 0 :(得分:0)

尝试使用

User.findOrCreate({
    raw: true,
    where: query,
    defaults: update,
    include: [{
      model: Model.Tag,
      as: 'tags',
      required: false // <-- thanks to this will return null if none is joined
    }]
  })

参考:http://docs.sequelizejs.com/manual/tutorial/models-usage.html#nested-eager-loading