Waterline:创建或更新填充记录

Waterline: Create or update a populate record

我的问题很基础,但我在文档中找不到答案。我有两个模型:'Person' 和 'Location' 一对一关联。 我想在 Location 集合中创建或更新 'populated child records'。

 let person = await Person.findOne({ phone: inputs.phone }).populate('reside');

嗯return

{ reside: [],
createdAt: 1540081110195,
updatedAt: 1540331824622,
id: '5bcbc5d609618e2cfbe64883',
phone: '+42424242',}

我想创建新的位置记录(当它们不存在时)或者如果它们存在则更新。我试试

let location = await Location.update({ id: person.reside.id })
.set({city: inputs.city,
    street: inputs.street,
  }).fetch;

但是在还没有创建记录的情况下不起作用。

model/Person.js

phone: {
  type: 'string',
  unique: true,
},
reside: {
  collection:'location',
  via: 'owner'
},

models/Location.js

city: { type: 'string' },
street: { type: 'string' },
owner:{
  model:'person',
  unique: true
}

我用action2

我终于找到 here methods .addToCollection(), .removeFromCollection(), or .replaceCollection() 来修改特定记录或记录集的填充值。它似乎不是文档中谈论它的最合适的地方