【问题标题】:Sails.js - How to update model with associationsSails.js - 如何使用关联更新模型
【发布时间】:2015-02-27 21:38:02
【问题描述】:

我正在尝试使用具有关联的模型。我有两个模型定义如下:

User.js

module.exports = {schema: true, attributes: {
   userId: {
       autoIncrement: true,
       type: integer,
       primaryKey: true,
   },
   userId: {type: integer},
   email: {type:string, size:200},
   addressId: {
      model:UserAddress
   }
}

Address.js

module.exports = {schema: true, attributes: {
  addressId: {
     autoIncrement: true,
     type:integer,
     primaryKey:true
  },
  userId: {type:integer},
  address: {type:string, size:250}
}

到目前为止,我能够通过将以下 JSON POST 到

来在两个模型中创建记录
http://localhost:1337/user/

{
    "userName": "John",
    "userId": "3",
    "email":"john@hotmail.com",
    "addressId": {
        "userId": "3",
        "address": "123 abc street"
     }
 }

我能够使用带有此 URL 的 PUT 更新两条记录: http://localhost:1337/user/update/1和下面的 JSON,请注意,在这种情况下,我在嵌套的 JSON 中添加了 addressId

{
    "userName": "John",
    "userId": "3",
    "accountLogin":"john@hotmail.com",
    "addressId": {
        "addressId":1,
        "userId": "3",
        "streetAddress": "123 abc street"
     }
}

但是,当我第一次在 User 表中创建一条没有地址的记录,后来又想添加它时,我没有运气让它工作。我所做的是对http://localhost:1337/user/update/1 使用PUT 和以下JSON,因为地址是新的,所以没有addressId。

{
    "userName": "John Doe",
    "accountLogin":"john@hotmail.com",
    "addressId": {
        "userId": "3",
        "streetAddress": "123 abc street"
     }
}

有没有办法做我在 Sails.js 中尝试做的事情?我可以发布到http://localhost:1337/address 来创建新记录,但我想让我的 API 保持一致是可能的。更严重的问题是,当上面的 JSON 被发送时,它崩溃了后端。在我看来,用户可以通过发送无效的 JSON 轻松地使后端崩溃,我将如何保护后端免受这种崩溃?

【问题讨论】:

  • 我只是在考虑您的问题,并想出了一件事可能有助于进一步调查:您使用的是哪个适配器,您是否尝试使用另一个适配器。
  • 我正在使用 mysql。错误消息看起来与适配器无关,它说:“/jzj/node_modules/sails/node_modules/waterline/lib/waterline/utils/nestedOperations/update.js:532 this.create(record).exec(function(err , val) { ^ TypeError: Object # has no method 'create' at createRecord (/Users/mcheng/jzj/node_modules/sails/node_modules/waterline/lib/waterline ... node_modules/async/lib/async. js:125:13 at Array.forEach (native) at _each (/Users/mcheng/jzj/node_modules/sails/node_modules/waterline/node_modules/async/lib/async.js:46:24)"
  • 您可以在使用水线默认主键时尝试吗?而且您也没有完全使用一对一关联,因为您的地址中没有关联反射,因为您的“userId”字段代表一个整数它应该是一个合适的“用户”userId : { model : user }
  • 您的update 代码是什么样的?您使用的是User.find,还是使用User.update

标签: sails.js waterline model-associations


【解决方案1】:

喂,

要将新对象添加到对象内的集合中,您可以使用内置(蓝图)“添加”方法的风帆。我检查了网站上的 Sails 文档,但没有完全成功,但是如果您检查 github 存储库中的 add 蓝图,它完全符合您的要求。

方法描述如下:

 /**
 * Add Record To Collection
 *
 * post  /:modelIdentity/:id/:collectionAttr/:childid
 *  *    /:modelIdentity/:id/:collectionAttr/add/:childid
 *
 * Associate one record with the collection attribute of another.
 * e.g. add a Horse named "Jimmy" to a Farm's "animals".
 * If the record being added has a primary key value already, it will
 * just be linked.  If it doesn't, a new record will be created, then
 * linked appropriately.  In either case, the association is bidirectional.
 *
 * @param {Integer|String} parentid  - the unique id of the parent record
 * @param {Integer|String} id    [optional]
 *        - the unique id of the child record to add
 *        Alternatively, an object WITHOUT a primary key may be POSTed
 *        to this endpoint to create a new child record, then associate
 *        it with the parent.
 *
 * @option {String} model  - the identity of the model
 * @option {String} alias  - the name of the association attribute (aka "alias")
 */

要回答你的下一个问题,是的,还有一个删除方法;)

你可以在那里找到更多信息:https://github.com/balderdashy/sails/tree/master/lib/hooks/blueprints/actions

【讨论】:

  • 感谢 MrVinz,我希望 CRUD 能够自动处理这种情况。它只是客户端上的更新操作,客户端不需要知道地址是如何存储在后端的。最大的问题是,正如我在帖子末尾提到的,如果客户端在这种情况下简单地调用更新,它会使服务器崩溃。我很想知道如何处理这种错误(CRUD 中发生的错误)。
  • 继续我之前的评论,“add”方法仅适用于关联的集合类型(一对多)。 Sailsjs.org 上的蓝图 API 参考提到,对于一对一关联,只需使用 Update (sailsjs.org/#/documentation/reference/blueprint-api/Update.html)。但它没有给出更新现有记录并同时添加新关联的示例。
  • 当您的服务器崩溃时,您会收到任何错误吗?
  • 查看我对上面原始帖子的评论。
猜你喜欢
  • 1970-01-01
  • 2013-09-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 2016-01-22
相关资源
最近更新 更多