1

所以这个问题感觉很像我的上一个问题。Strongloop 让我觉得自己像个菜鸟。哈哈。

我从一些 LB 示例中偷了这个:

var Order = mongoDev.createModel('order', {
    customerId: Number,
    orderDesc: String
});

var Customer = mongoDev.createModel('customer', {
    id: {type: Number, id: true},
    name: String,
    emails: [String],
    age: Number},
  {strcit: true});

//Order.belongsTo(Customer);
Order.belongsTo(Customer, {as: 'customer', foreignKey: 'customerId'});
Customer.hasMany(Order, {as: 'orders', foreignKey: 'customerId'});

在资源管理器中,我看到了客户和订单端点。客户也有端点/customers/{id}/orders,订单有/orders/{id}/customer等等。到目前为止一切都很好。

我有这个客户添加:

GET .../api/customers/53c599594e23e50000a41acf
{
  "id": "53c599594e23e50000a41acf",
  "name": "John1",
  "emails": [
    "john@x.com",
    "jhon@y.com"
  ],
  "age": 30
}

当我发布订单时,我得到一个空的 customerId:

POST .../api/customers/53c599594e23e50000a41acf/orders
    data: {"orderDesc": "whatever"}

result:
{
  "customerId": null,
  "orderDesc": "whatever",
  "id": "53c59d1efd31a4000062e7d8"
}

有什么想法我在这里做错了吗?

4

1 回答 1

2

好的,解决了这个问题。我从中窃取的示例将 ID 键入为数字。将它们更改为字符串为我解决了这个问题。

var Order = mongoDev.createModel('order', {
    customerId: String,
    orderDesc: String
});

var Customer = mongoDev.createModel('customer', {
    id: {type: String, id: true},
    name: String,
    emails: [String],
    age: Number},
  {strcit: true});
于 2014-07-16T00:33:41.953 回答