使用单个请求在 SailsJS 中创建具有一对多关联的新记录

问题描述

从sails 文档outlined here 看来,如果我应该能够设置具有关联的模型,然后使用创建蓝图在单个请求中创建父记录和关联记录。

我使用的是 Sails v1.4

客户模型

/**
 * Client.js
 *
 * @description :: A model deFinition represents a database table/collection.
 * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
 */

module.exports = {
  attributes: {

    name: {
      type: 'string',required: true,unique: true
    },address: {
      type: 'string',required: false,allowNull: true
    },/**
     * Associations
     */

    people: {
      collection: 'ClientContact',via: 'company'
    }

  },};

客户联系模型

/**
 * ClientContact.js
 *
 * @description :: A model deFinition represents a database table/collection.
 * @docs        :: https://sailsjs.com/docs/concepts/models-and-orm/models
 */

module.exports = {

  attributes: {

    name: {
      type: 'string',required: true
    },email: {
      type: 'string',allowNull: true,isEmail: true
    },/**
     * Associations
     */

    company: {
      model: 'client'
    }

  }

};

发布

{
    "name": "Bobs Plumbing","address": "123 Some Road,Somewheresville","people": [
        {
            "name": "Joe Bloggs","email": "joe@bobsplumbing.com"
        }
    ]
}

失败并出现错误

无法使用提供的 where 子句。无法按 id 过滤: in 修饰符数组中的项目无效。与声明不符 对应属性的数据类型。 1 错误验证值: • 指定值(字典:{ name: 'Joe Bloggs',email: 'joe@bosplumbing.com' }) 与预期类型不匹配:'number'

我期望创建一个新的 Client 记录,以及创建一个与该客户端相关联的新 ClientContact 记录。

相关问题

这个问题似乎验证了我的期望,但似乎不起作用。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)