Search code examples
javascriptjsonmodelloopbackjs

scope property is not being applied to loopback model


I have used an "include" : "organization" query in the scope of my request.json file, which is a related model. But, the relation is not being included in the resulting output from a query. The model (request.json file) looks like...

{
  "name": "request",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "amount": {
      "type": "number",
      "required": true
    },
    "deadline": {
      "type": "date",
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "organization": {
      "type": "belongsTo",
      "model": "organization",
      "foreignKey": "",
      "options": {
        "nestRemoting": true
      }
    }
  },
  "scope" : {
    "include" : "organization"
  }
}

Solution

  • In order to include another model, you must have a foreignKey defined in your model relations.

      "relations": {
        "organization": {
          "type": "belongsTo",
          "model": "organization",
          "foreignKey": "organizationId",
          "options": {
            "nestRemoting": true
          }
        }
      },
    

    set the name of the foreignKey that you want to use. organizationId in this case, and this field will be added to your model request