hasMany relation loopback - node.js

My models.json:
My models.json
"masterTrip": {
"options": {
"relations": {
"trips": {
"type": "hasMany",
"model": "trip",
"foreignKey": "masterTripId"
}
}
}
},
"trip": {
"options": {
"relations": {
"masterTrips": {
"type": "belongsTo",
"model": "masterTrip",
"foreignKey": "masterTripId"
}
}
}
},
But I do not get the relation between the trip and mastertrip. can anybody explain?

There are a couple things that might be the problem. Here is what comes to mind:
You should only need the foreign key on the model that belongsTo the other one. (In this case, that would be trip.)
Have you actually created trips underneath masterTrip? (Either in the code itself or via the API?) I know this sounds silly, but the context wasn't clear enough for me to tell if you had created the sample data or not.
It sounds like you might actually be getting data when you do a GET on /masterTrip/1/trips Is that right? If so , then that sounds like the correct behavior.
I'm still relatively new to LoopBack myself, but I'm not sure that filter[include]=belongsToRelationName is the correct way to get the data you want. Technically, you are just looking for the associated array of hasMany data, right? In this case, trips that belongTo masterTrip. The RESTful way to get that would be masterTrip/{id}/trips
Hope one of those helps.

Your "belongs to" relation name is not singular. It should be singular.
When you making " belongs to " relation name is singular and for hasMany your relation name in plural. Please see the official documentation for more details -
See this working example below -
{
"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": {}
}
{
"name": "Bidding",
"base": "PersistedModel",
"strict": false,
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"myuserId": {
"type": "number"
},
"bookingId": {
"type": "number"
},
"booking_no": {
"type": "string"
},
"price": {
"type": "string"
},
"message": {
"type": "string"
},
"bid_date": {
"type": "string"
},
"bid_time": {
"type": "string"
},
"bid_status": {
"type": "string"
},
"lang": {
"type": "string"
},
"rate_driver": {
"type": "number"
},
"created": {
"type": "string"
},
"modified": {
"type": "string"
}
},
"validations": [],
"relations": {
"booking": {
"type": "belongsTo",
"model": "Booking",
"foreignKey": "bookingId"
},
"myuser": {
"type": "belongsTo",
"model": "Myuser",
"foreignKey": "myuserId"
}
},
"acls": [],
"methods": {}
}

Related

Azure Logic Apps, Parsed JSON returning Null when used

I'm trying to access the values in JSON output that I received from two (2) Graph API calls, but each time I try to use them I get this error:
ExpressionEvaluationFailed. The execution of template action 'For_each' failed: the result of the evaluation of 'foreach' expression '#body('Parse_JSON_-_Managed_Devices')?['body']?['value']' is of type 'Null'. The result must be a valid array.
I have validated that my Graph API calls are properly formatted, and output is exactly what I'm expecting to be returned from both API calls. I get this error every time I either try to access the parsed JSON in an Azure Runbook or any other Logic App tasks.
I would love to know if someone has experienced this before and how it can be solved?
Graph query: https://graph.microsoft.com/beta/deviceManagement/managedDevices/?$select=id,userId,deviceName,userDisplayName,azureADDeviceId,managedDeviceName,emailAddress&$filter=operatingSystem eq 'windows'
JSON schema for managed devices
{
"properties": {
"body": {
"properties": {
"##odata.context": {
"type": "string"
},
"##odata.count": {
"type": "integer"
},
"##odata.nextLink": {
"type": "string"
},
"value": {
"items": {
"properties": {
"azureADDeviceId": {
"type": "string"
},
"deviceName": {
"type": "string"
},
"emailAddress": {
"type": "string"
},
"id": {
"type": "string"
},
"managedDeviceName": {
"type": "string"
},
"userDisplayName": {
"type": "string"
},
"userId": {
"type": "string"
}
},
"required": [
"id",
"userId",
"deviceName",
"userDisplayName",
"azureADDeviceId",
"managedDeviceName",
"emailAddress"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
}
Graph query: https://graph.microsoft.com/beta/users?$select=id,displayName,mail,officeLocation&$filter=accountEnabled eq true
JSON schema used for users
Graph query: https://graph.microsoft.com/beta/users?$select=id,displayName,mail,officeLocation&$filter=accountEnabled eq true
{
"properties": {
"body": {
"properties": {
"##odata.context": {
"type": "string"
},
"##odata.nextLink": {
"type": "string"
},
"value": {
"items": {
"properties": {
"displayName": {
"type": "string"
},
"id": {
"type": "string"
},
"mail": {
"type": "string"
},
"officeLocation": {
"type": "string"
}
},
"required": [
"id",
"displayName",
"mail",
"officeLocation"
],
"type": "object"
},
"type": "array"
}
},
"type": "object"
}
},
"type": "object"
}

How to insert array value into table using logic app

when a http request is received. i need to insert the array value into a table. in my case the array is response required.
I used these things: when a http request is received and i used parse JSON and i used for each loop then inside the for-each i used insert entity but it's throwing an error. if anybody knows how to implement let me know the answer.
i used expression for RRT as : body('Parse_JSON')['ResponseRequired'][0]['ResponseRequiredType']
json schema
{
"$schema": "http://json-schema.org/draft-04/schema#",
"properties": {
"AssetErrorCode": {
"type": "string"
},
"AssetErrorDesc": {
"type": "string"
},
"AssetId": {
"type": "integer"
},
"CustomerId": {
"type": "integer"
},
"ResponseRequired": {
"items": [
{
"properties": {
"ResponseRequiredAdditionalData": {
"type": "string"
},
"ResponseRequiredAddress": {
"type": "string"
},
"ResponseRequiredFrequency": {
"type": "string"
},
"ResponseRequiredType": {
"type": "integer"
}
},
"required": [
"ResponseRequiredType",
"ResponseRequiredFrequency",
"ResponseRequiredAddress",
"ResponseRequiredAdditionalData"
],
"type": "object"
},
{
"properties": {
"ResponseRequiredAdditionalData": {
"type": "string"
},
"ResponseRequiredAddress": {
"type": "string"
},
"ResponseRequiredFrequency": {
"type": "string"
},
"ResponseRequiredType": {
"type": "integer"
}
},
"required": [
"ResponseRequiredType",
"ResponseRequiredFrequency",
"ResponseRequiredAddress",
"ResponseRequiredAdditionalData"
],
"type": "object"
},
{
"properties": {
"ResponseRequiredAdditionalData": {
"type": "string"
},
"ResponseRequiredAddress": {
"type": "string"
},
"ResponseRequiredFrequency": {
"type": "string"
},
"ResponseRequiredType": {
"type": "integer"
}
},
"required": [
"ResponseRequiredType",
"ResponseRequiredFrequency",
"ResponseRequiredAddress",
"ResponseRequiredAdditionalData"
],
"type": "object"
}
],
"type": "array"
},
"ServiceKey": {
"type": "string"
}
},
"required": [
"CustomerId",
"ServiceKey",
"AssetId",
"AssetErrorCode",
"AssetErrorDesc",
"ResponseRequired"
],
"type": "object"
}
used this expression : items('For_each')?['ResponseRequiredAddress']
solved the issue

Loopback relation

How to do the relation to match with an array of data in loopback
For e.g
My Models
// Regions model
{
"name": "regions",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"title": {
"type": "string",
"required": true
},
"images": {
"type": [
{
"target_id": {
"type": "string"
}
}
]
},
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
// Images model
{
"name": "images",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"url": {
"type": "string",
"required": true
}
},
"validations": [],
"relations": { },
"acls": [],
"methods": {}
}
Expected output:
[
{
"title": "Region 1",
"field_images": [{
"name": "Image 2",
"url": "/media/image-1600x650.jpg",
},{
"name": "Image 1",
"url": "/media/image-1600x650.jpg",
}]
} ]
Assuming that the relationship is HasMany
{
"name": "regions",
"base": "PersistedModel",
...
"relations": {
"images": {
"type": "hasMany",
"model": "images",
"foreignKey": "title", //region title
}
}
...
}
A relationship is defined between two models. So, I highly doubt you would get the images inside an array (if you want to define a relationship between them).
But on loopback you can use separate APIs to retrieve the field_images instead of getting it as an array.
For example - you could retrieve the images like this
region.images([filter],
function(err, images) {
...
});

StrongLoop REST API "hasManyThrough" include filter always returns empty

Premise
Pretty short and simple. In my testing/learning environment for StrongLoop, I have set up two models: CoffeeShop and Person. Persons can have many "employers", and CoffeeShops can have many "employees". This relationship is maintained by a "hasMany"/"through" type relationship on both the CofeeShop and Person models.
Problem
When querying either model through the REST API, an include filter always returns an empty array for the associated relation, even if relations exist. In other words, the API call
http://localhost:3000/api/CoffeeShops/67/employees
works as expected, but
GET: http://localhost:3000/api/CoffeeShops/67/?filter={"include":["employees"]}
Does not.
Could anyone help me figure out why this is? I've attached pictures and the .json model definitions files as well.
// /common/models/coffee-shop.json
{
"name": "CoffeeShop",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"city": {
"type": "string",
"required": true
},
"numberEmployees": {
"type": "number"
},
"ownerId": {
"type": "number"
},
"isSmallBusiness": {
"type": "boolean"
}
},
"validations": [],
"relations": {
"owner": {
"type": "belongsTo",
"model": "Person",
"foreignKey": ""
},
"employees": {
"type": "hasMany",
"model": "Person",
"foreignKey": "employerId",
"through": "CoffeeShopPersonEmployeeEmployer"
}
},
"acls": [],
"methods": {}
}
// /omon/models/person.json
{
"name": "Person",
"plural": "Persons",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {
"name": {
"type": "string",
"required": true
},
"age": {
"type": "number"
}
},
"validations": [],
"relations": {
"shops": {
"type": "hasMany",
"model": "CoffeeShop",
"foreignKey": "ownerId"
},
"employers": {
"type": "hasMany",
"model": "CoffeeShop",
"foreignKey": "employeeId",
"through": "CoffeeShopPersonEmployeeEmployer"
}
},
"acls": [],
"methods": {}
}
// /common/models/coffee-shop-person-employee-employer.json
{
"name": "CoffeeShopPersonEmployeeEmployer",
"base": "PersistedModel",
"idInjection": true,
"options": {
"validateUpsert": true
},
"properties": {},
"validations": [],
"relations": {
"person": {
"type": "belongsTo",
"model": "Person",
"foreignKey": "employeeId"
},
"coffeeShop": {
"type": "belongsTo",
"model": "CoffeeShop",
"foreignKey": "employerId"
}
},
"acls": [],
"methods": {}
}
Images of REST requests
in your CoffeeShopPersonEmployeeEmployer Model:
add foreign keys properties:
"properties": {
"CoffeeShopId": {
"type": "number",
"required": true
},
"PersonId": {
"type": "number",
"required": true
}
}
and ajust the relations :
"relations": {
"person": {
"type": "belongsTo",
"model": "Person",
"foreignKey": "PersonId"
},
"coffeeShop": {
"type": "belongsTo",
"model": "CoffeeShop",
"foreignKey": "CoffeeShopId"
}
}

Model Relation in Loopback framework

I want to establish a relation between two models, following the json files:
{
"name": "Surveyor",
"plural": "surveyors",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"mongodb": {
"collection": "surveyors"
},
"settings": {
"mongodb": {
"allowExtendedOperators": true
}
},
"properties": {
"name": {
"type": "string",
"required": true
},
"surname": {
"type": "string",
"required": true
},
"dateOfBirth": {
"type": "date"
},
"birthPlace": {
"type": "string"
},
"provinceOfBirth": {
"type": "string"
},
"gender": {
"type": "string"
},
"registrationNumber": {
"type": "string",
"required": true,
"unique": true
}
},
"validations": [],
"relations": {}
},
"acls": [],
"methods": {}
}
{
"name": "Dossier",
"plural": "dossiers",
"base": "PersistedModel",
"strict": true,
"idInjection": true,
"options": {
"validateUpsert": true
},
"mongodb": {
"collection": "dossiers"
},
"settings": {
"mongodb": {
"allowExtendedOperators": true
}
},
"properties": {
"delCode": {
"type": "string",
"required": true
},
"client": {
"name": {
"type": "string",
"required": true
},
"surname": {
"type": "string",
"required": true
},
"taxCode": {
"type": "string"
}
},
"surveyor": {
"type": "string"
},
"examination": {
"type": "object"
},
"appointment": {
"type": "date"
},
"status": {
"type": "string",
"required": true
},
"notes": {
"type": "string"
}
},
"validations": [],
"relations": {},
"acls": [],
"methods": {}
}
One dossier can have one surveyor, as key I want to use the property surveyor in the Dossier Model, corresponding to the property registrationNumber in the Surveyor Model.
I want to query the dossier Model and obtain the surveyor data in the dossier doc:
[GET] /api/dossiers
or with a filter:
[GET] /api/dossiers?filter={"include":"surveyor"}
if it is possible I want to configure the relation in the json file, not in the code.
You can use the relation generator to create that for you. In your case you're going to need to create a belongsTO relation from Dossier to Surveyor. If you want to query the other way around (dossiers from a surveyor) you're going to need to create a hasMany relation from Surveyor to Dossier.
In short, for what you've asked, you're going to end up with something like this:
In Dossier model:
...
"relations": {
"surveyor": {
"type": "belongsTo",
"model": "Surveyor",
"foreignKey": "surveyor",
"primaryKey": "registrationNumber"
}
}
...

Resources