我正在尝试使用Strongloop loopback和loopback-component-passport但是无法弄清楚如何获取内置User模型和loopback-component-passport提供的模型之间的关系来工作< / p>
看起来这些关系是在loopback-component-passport / lib / models / * .json文件中提供的,但是我收到以下错误:
Error: Cyclic dependency: "User"
at visit (/work/node/loopback/myapp/node_modules/loopback-boot/node_modules/toposort/index.js:29:13)
at visit (/work/node/loopback/myapp/node_modules/loopback-boot/node_modules/toposort/index.js:43:9)
at toposort (/work/node/loopback/myapp/node_modules/loopback-boot/node_modules/toposort/index.js:22:22)
at module.exports.exports (/work/node/loopback/myapp/node_modules/loopback-boot/node_modules/toposort/index.js:10:10)
at sortByInheritance (/work/node/loopback/myapp/node_modules/loopback-boot/lib/compiler.js:248:21)
at buildAllModelInstructions (/work/node/loopback/myapp/node_modules/loopback-boot/lib/compiler.js:205:10)
at compile (/work/node/loopback/myapp/node_modules/loopback-boot/lib/compiler.js:79:27)
at bootLoopBackApp (/work/node/loopback/myapp/node_modules/loopback-boot/index.js:128:22)
at Object.<anonymous> (/work/node/loopback/myapp/server/server.js:44:1)
at Module._compile (module.js:456:26)
[stu:/work/node/loopback/myapp (master)]$
这是我的model-config.json
{
"_meta": {
"sources": [
"loopback/common/models",
"loopback/server/models",
"../common/models",
"./models",
"../node_modules/loopback-component-passport/lib/models"
]
},
"User": {
"dataSource": "db",
"public": true
},
"UserCredential": {
"dataSource": "db",
"public": true
},
"UserIdentity": {
"dataSource": "db",
"public": true
},
"AccessToken": {
"dataSource": "db",
"public": false
},
"ACL": {
"dataSource": "db",
"public": false
},
"RoleMapping": {
"dataSource": "db",
"public": false
},
"Role": {
"dataSource": "db",
"public": false
},
"KnowBit": {
"dataSource": "localpg",
"public": true
}
}
通过loopback启用调试:boot:compiler
$ DEBUG=loopback:boot:compiler slc run
我能够获得这些日志:
[stu:/work/node/loopback/myapp (master)]$ DEBUG=loopback:boot:compiler slc run
INFO strong-agent API key not found, StrongOps dashboard reporting disabled.
Generate configuration with:
npm install -g strongloop
slc strongops
See http://docs.strongloop.com/strong-agent for more information.
supervisor running without clustering (unsupervised)
loopback:boot:compiler Found model "AccessToken" - ../node_modules/loopback/common/models/access-token.json ../node_modules/loopback/common/models/access-token.js +0ms
loopback:boot:compiler Found model "ACL" - ../node_modules/loopback/common/models/acl.json ../node_modules/loopback/common/models/acl.js +1ms
loopback:boot:compiler Found model "Application" - ../node_modules/loopback/common/models/application.json ../node_modules/loopback/common/models/application.js +0ms
loopback:boot:compiler Found model "Change" - ../node_modules/loopback/common/models/change.json ../node_modules/loopback/common/models/change.js +1ms
loopback:boot:compiler Found model "Checkpoint" - ../node_modules/loopback/common/models/checkpoint.json ../node_modules/loopback/common/models/checkpoint.js +0ms
loopback:boot:compiler Found model "Email" - ../node_modules/loopback/common/models/email.json ../node_modules/loopback/common/models/email.js +0ms
loopback:boot:compiler Found model "RoleMapping" - ../node_modules/loopback/common/models/role-mapping.json ../node_modules/loopback/common/models/role-mapping.js +0ms
loopback:boot:compiler Found model "Role" - ../node_modules/loopback/common/models/role.json ../node_modules/loopback/common/models/role.js +1ms
loopback:boot:compiler Found model "Scope" - ../node_modules/loopback/common/models/scope.json ../node_modules/loopback/common/models/scope.js +0ms
loopback:boot:compiler Found model "User" - ../node_modules/loopback/common/models/user.json ../node_modules/loopback/common/models/user.js +0ms
loopback:boot:compiler Skipping unknown module source dir "loopback/server/models" +0ms
loopback:boot:compiler Found model "KnowBit" - ../common/models/know-bit.json ../common/models/know-bit.js +1ms
loopback:boot:compiler Model source code not found: undefined - TypeError: Arguments to path.join must be strings +0ms
loopback:boot:compiler Found model "User" - ../common/models/user.json (no source file) +0ms
loopback:boot:compiler Skipping unknown module source dir "./models" +0ms
loopback:boot:compiler Found model "ApplicationCredential" - ../node_modules/loopback-component-passport/lib/models/application-credential.json ../node_modules/loopback-component-passport/lib/models/application-credential.js +1ms
loopback:boot:compiler Found model "UserCredential" - ../node_modules/loopback-component-passport/lib/models/user-credential.json ../node_modules/loopback-component-passport/lib/models/user-credential.js +0ms
loopback:boot:compiler Found model "UserIdentity" - ../node_modules/loopback-component-passport/lib/models/user-identity.json ../node_modules/loopback-component-passport/lib/models/user-identity.js +0ms
loopback:boot:compiler Using model "User"
Configuration: {"dataSource":"db","public":true}
Definition {"name":"User","plural":"users","base":"User","relations":{"accessTokens":{"type":"hasMany","model":"AccessToken","foreignKey":"userId"},"identities":{"type":"hasMany","model":"UserIdentity","foreignKey":"userId"},"credentials":{"type":"hasMany","model":"UserCredential","foreignKey":"userId"}},"validations":[],"acls":[],"methods":[]} +0ms
loopback:boot:compiler Using model "UserCredential"
Configuration: {"dataSource":"db","public":true}
Definition {"name":"UserCredential","base":"PersistedModel","properties":{"provider":{"type":"String","comments":"facebook, google, twitter, linkedin"},"authScheme":{"type":"String","comments":"oAuth, oAuth 2.0, OpenID, OpenID Connect"},"externalId":{"type":"String","comments":"The provider specific id"},"profile":{"type":"Object"},"credentials":{"type":"Object"},"created":"Date","modified":"Date","id":{"id":1,"generated":true}},"acls":[{"principalType":"ROLE","principalId":"$everyone","permission":"DENY"},{"principalType":"ROLE","principalId":"$owner","permission":"ALLOW"}],"relations":{"user":{"type":"belongsTo","model":"User","foreignKey":"userId"}}} +1ms
loopback:boot:compiler Using model "UserIdentity"
Configuration: {"dataSource":"db","public":true}
Definition {"name":"UserIdentity","plural":"UserIdentities","base":"PersistedModel","properties":{"provider":{"type":"String","comments":"facebook, google, twitter, linkedin"},"authScheme":{"type":"String","comments":"oAuth, oAuth 2.0, OpenID, OpenID Connect"},"externalId":{"type":"String","comments":"The provider specific id"},"profile":{"type":"Object"},"credentials":{"type":"Object"},"created":"Date","modified":"Date","id":{"id":1,"generated":true}},"acls":[{"principalType":"ROLE","principalId":"$everyone","permission":"DENY"},{"principalType":"ROLE","principalId":"$owner","permission":"ALLOW"}],"relations":{"user":{"type":"belongsTo","model":"User","foreignKey":"userId"}}} +0ms
loopback:boot:compiler Using model "AccessToken"
Configuration: {"dataSource":"db","public":false}
Definition {"name":"AccessToken","properties":{"id":{"type":"string","id":true},"ttl":{"type":"number","ttl":true,"default":1209600,"description":"time to live in seconds (2 weeks by default)"},"created":{"type":"Date"}},"relations":{"user":{"type":"belongsTo","model":"User","foreignKey":"userId"}},"acls":[{"principalType":"ROLE","principalId":"$everyone","permission":"DENY"},{"principalType":"ROLE","principalId":"$everyone","property":"create","permission":"ALLOW"}]} +0ms
loopback:boot:compiler Using model "ACL"
Configuration: {"dataSource":"db","public":false}
Definition {"name":"ACL","properties":{"model":{"type":"string","description":"The name of the model"},"property":{"type":"string","description":"The name of the property, method, scope, or relation"},"accessType":"string","permission":"string","principalType":"string","principalId":"string","id":{"id":1,"generated":true}}} +0ms
loopback:boot:compiler Using model "RoleMapping"
Configuration: {"dataSource":"db","public":false}
Definition {"name":"RoleMapping","description":"Map principals to roles","properties":{"id":{"type":"string","id":true,"generated":true},"principalType":{"type":"string","description":"The principal type, such as user, application, or role"},"principalId":"string"},"relations":{"role":{"type":"belongsTo","model":"Role","foreignKey":"roleId"}}} +0ms
loopback:boot:compiler Using model "Role"
Configuration: {"dataSource":"db","public":false}
Definition {"name":"Role","properties":{"id":{"type":"string","id":true,"generated":true},"name":{"type":"string","required":true},"description":"string","created":"date","modified":"date"},"relations":{"principals":{"type":"hasMany","model":"RoleMapping","foreignKey":"roleId"}}} +0ms
loopback:boot:compiler Using model "KnowBit"
Configuration: {"dataSource":"localpg","public":true}
Definition {"name":"KnowBit","base":"PersistedModel","idInjection":true,"properties":{"label":{"type":"string","required":true},"link":{"type":"string","required":true}},"validations":[],"relations":{},"acls":[],"methods":[]} +0ms
答案 0 :(得分:3)
TL; DR
我为名称和基本属性指定了相同的字符串“User”,以解决问题我将名称值更改为小写“user”
全文
好的,所以我复制了
loopback-example-passport/common/models/user.json
文件我输入错误并设置名称='用户'而不是'用户'
这并没有解决最终问题,但我认为它解释了Cyclic依赖性。 'base'是这个模型的'基础'类,我将其设置为'User'以及
这允许我超越模型编译阶段,但仍然没有配置关系,从“用户”到护照代码所需的“UserIdentity”。
我认为我现在正走在正确的轨道上,洞察力是strongloop / loopback-example-passport示例中的环回模型是使用新的模型类覆盖内置类,其中类名的第一个字母是较低的例如userCredential是内置模型类UserCredential的覆盖等等,这有点令人困惑
{
"name": "userCredential",
"plural": "userCredentials",
"base": "UserCredential",
...
但我认为如果我通过并检查我是否正在使用小写版本,那么事情可能会解决
这确实是问题