我处理MySQL DB的应用程序链接。我有不同的模型,我想在另一个model.js中使用模型。
例如我有2个模型:
Server.json
{
"name": "server",
"base": "PersistedModel",
"idInjection": false,
"properties": {
"idserver": {
"type": "number",
"id": true
},
"Name": {
"type": "string"
},
"type": {
"type": "string"
},
"cpus": {
"type": "number"
},
"memory": {
"type": "number"
},
"storage": {
"type": "number"
},
"hypervisor": {
"type": "number"
},
"iddatastore": {
"type": "number"
},
"comment": {
"type": "string"
}
},
"validations": [],
"relations": {
"datastores": {
"type": "hasAndBelongsToMany",
"model": "Datastore"
}
},
"acls": [],
"methods": []
}
和Datastore.json
{
"name": "Datastore",
"plural":"Datastores",
"base": "PersistedModel",
"idInjection": false,
"properties": {
"iddatastore": {
"type": "number",
"id": true
},
"owner": {
"type": "number"
},
"size": {
"type": "number"
},
"type": {
"type": "string"
},
"name": {
"type": "string"
}
},
"validations": [],
"relations": {
"servers": {
"type": "hasAndBelongsToMany",
"model": "server"
}
},
"acls": [],
"methods": []
}
我希望从Server.js中的Datastore获取数据。
我尝试这种方式,但效果不好:
Server.js
var loopback = require('loopback');
var app = module.exports = loopback();
module.exports = function(Server) {
Server.on('attached',function(){
var override = Server.find;
var Datastore = Server.app.models.Datastore;
// the problem is here. It seems to return the description of the model when i look for the data
Server.findById = function(filter,callback){
var id = arguments[0];
// this isn't working
console.log(Datastore.find({where:{"idserver":id}}));
return override.apply(this, arguments);
};
});
};
我知道这有可能以不同的方式,但如果有可能我更喜欢从Datastore获取数据!
对不起我的英语,如果不清楚,你有什么想法吗?
Loïc
答案 0 :(得分:0)
尝试var Datastore = loopback.getModel('Datastore');