Loopback issues when adding relations

时间:2016-04-25 08:50:02

标签: json node.js models loopbackjs

I have a Loopback API in which I have a container and I'm trying to add some relations to my models (using this) but when it comes to adding relations to container.js, it does not work anymore and I get this error :

/home/mowso/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:434
          modelClass[relation.type].call(modelClass, name, params);
                                   ^

TypeError: Cannot read property 'call' of undefined
    at EventEmitter.<anonymous> (/home/mowso/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:434:36)
    at EventEmitter.g (events.js:260:16)
    at emitOne (events.js:77:13)
    at EventEmitter.emit (events.js:169:7)
    at DataSource.setupDataAccess (/home/mowso/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:540:14)
    at DataSource.attach (/home/mowso/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/datasource.js:752:8)
    at Function.ModelClass.attachTo (/home/mowso/Documents/loopback-examples/test/node_modules/loopback-datasource-juggler/lib/model-builder.js:336:16)
    at Registry.configureModel (/home/mowso/Documents/loopback-examples/test/node_modules/loopback/lib/registry.js:238:15)
    at configureModel (/home/mowso/Documents/loopback-examples/test/node_modules/loopback/lib/application.js:441:16)
    at EventEmitter.app.model (/home/mowso/Documents/loopback-examples/test/node_modules/loopback/lib/application.js:133:5)

I have three models like in the example I link above, they basically just have different names. Here is my container.json :

{
  "name": "Container",
  "base": "PersistedModel",
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
      "user": {
      "type": "belongsTo",
      "model": "user",
      "foreignKey": "ownerId"
    }
  },
  "acls": [],
  "methods": {}
}

and my user.json :

{
  "name": "user",
  "base": "User",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {},
  "validations": [],
  "relations": {
    "containers": {
      "type": "hasOne",
      "model": "Container",
      "foreignKey": "ownerId"
    },
    "companies": {
      "type": "hasOne",
      "model": "company",
      "foreignKey": "ownerId"
    }
  },
  "acls": [],
  "methods": {}
}

Here is the model-config.json :

{
  "_meta": {
    "sources": [
      "loopback/common/models",
      "loopback/server/models",
      "../common/models",
      "./models"
    ],
    "mixins": [
      "loopback/common/mixins",
      "loopback/server/mixins",
      "../common/mixins",
      "./mixins"
    ]
  },
  "User": {
    "dataSource": "db"
  },
  "AccessToken": {
    "dataSource": "db",
    "public": false
  },
  "ACL": {
    "dataSource": "db",
    "public": false
  },
  "RoleMapping": {
    "dataSource": "db",
    "public": false
  },
  "Role": {
    "dataSource": "db",
    "public": false
  },
  "Container": {
    "dataSource": "myfile",
    "public": true
  },
  "user": {
    "dataSource": "db",
    "public": false
  },
  "company": {
    "dataSource": "db",
    "public": false
  }
}

and finally my datasource.json, notice that I have a storage folder at the root of my project :

{
  "db": {
    "name": "db",
    "connector": "mongodb"
  },
  "myfile": {
    "name": "myfile",
    "connector": "loopback-component-storage",
    "provider": "filesystem",
    "root": "storage"
  }
}

2 个答案:

答案 0 :(得分:0)

Your foreign key is not correct, as per the documentation it should be,

Relation name: Camel case of the model name, for example, for the "supplier" model the relation is "supplier".

Foreign key: The relation name appended with ‘Id’, for example, for relation name "supplier" the default foreign key is "supplierId".

So in your case it should be

"foreignKey": "userId"

https://docs.strongloop.com/display/public/LB/HasOne+relations

And you should use the slc relations instead of editing the json files.

答案 1 :(得分:0)

我相信Container应该继承Model,而不是PersistedModel。默认情况下,Container是文件系统中的文件夹,因此无法选择是否保留它。

 {
    "name": "Container",
    "base": "Model",
相关问题