简体   繁体   中英

Issue with including related Model in loopback

I have two Models Customer and UserProviders

In Customer.json, have the following relation

"relations": {
    "userProviders": {
      "type": "hasOne",
      "model": "UserProviders",
      "foreignKey": "userId",
      "options": {
        "disableInclude": true,
        "http": {
          "path": "providers"
        },
        "hidden": [
          "id",
          "providersCount"
        ]
      }
    }
}

UserProviders.json

{
  "name": "UserProviders",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "userId": {
      "type": "objectId",
      "required": true,
      "index": {
        "unique": true
      }
    },
    "providers": {
      "type": [
        "any"
      ],
      "default": []
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": {}
}

This is how I tried to query the models in another Model say ModelB.

let users = ModelB.app.models.Customer;
let UserProvider = ModelB.app.models.UserProviders;

let userData = await users.findOne({
            where: {
            patientId: patientId
          },
            include: {
              relation: 'userProviders',
              scope: {
                fields: ['providers']
              }
            }
    });

Did a deep research on it. Checked the Joining two models in loopback using include filter result , but still not able to fix that.

Working on this for a couple of days. But still I am not getting the providers field included in userData. Any help would be really appreciated. How can i include the related model field? Please correct me if i am wrong.

"options": {
    "disableInclude": true
}

If you are using option: disableInclude to true, then it does not fetch the data if the relation is used in an include statement. So please try to remove it and check. Hope this will fix your issue.

For more info please refer loopback documentation: https://loopback.io/doc/en/lb3/Model-definition-JSON-file.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM