有很多关系回环

我的models.json:

我的models.json

"masterTrip": { "options": { "relations": { "trips": { "type": "hasMany", "model": "trip", "foreignKey": "masterTripId" } } } }, "trip": { "options": { "relations": { "masterTrips": { "type": "belongsTo", "model": "masterTrip", "foreignKey": "masterTripId" } } } }, 

但是我没有得到旅行和mastertrip之间的关系。 有人可以解释吗?

有几件事情可能是这个问题。 以下是想到的:

  • 您应该只需要属于另一个模型的外键。 (在这种情况下,这将是旅行 。)
  • 你真的创build了masterTrip下的行程? (无论是在代码本身或通过API?)我知道这听起来很愚蠢,但上下文不够清楚,我告诉你是否已经创build了样本数据。
  • 这听起来像你可能实际上正在获取数据,当你做一个GET /masterTrip/1/trips是吗? 如果是这样,那么这听起来像是正确的行为。

我还是比较新的LoopBack自己,但我不知道filter[include]=belongsToRelationName是正确的方式来获取您想要的数据。 从技术上讲,你只是在寻找hasMany数据的关联数组,对吧? 在这种情况下,属于masterTrip的行程 。 REST的方式来获得这将是masterTrip/{id}/trips

希望这些帮助之一。

你的“属于”关系名不是单一的。 它应该是单数。

当你使“属于”关系名称是单数,因为有许多你的关系名称在复数。 请参阅官方文档了解更多详情 –

看下面的这个工作例子 –

 { "name": "Booking", "base": "PersistedModel", "strict": false, "idInjection": true, "properties": { "myuserId": { "type": "number" }, "orgin": { "type": "string" }, "orgin_lat": { "type": "string" }, "orgin_lon": { "type": "string" }, "destination": { "type": "string" }, "dest_lat": { "type": "string" }, "dest_lon": { "type": "string" }, "parcel_type": { "type": "string" }, "volume": { "type": "string" }, "weight": { "type": "string" }, "price": { "type": "string" }, "receiver_phn": { "type": "string" }, "payment_mode": { "type": "string" }, "booking_status": { "type": "string" }, "lang": { "type": "string" }, "booking_no": { "type": "string" }, "cancel_reason": { "type": "string" }, "cancel_by": { "type": "string" }, "booking_date": { "type": "string" }, "plan_later": { "type": "string" }, "plan_date": { "type": "string" }, "created": { "type": "string" }, "modified": { "type": "string" } }, "validations": [], "relations": { "biddings": { "type": "hasMany", "model": "Bidding", "foreignKey": "bookingId" } }, "acls": [], "methods": {} }