Gan*_*jan 3 loopback strongloop loopbackjs
'use strict';
module.exports = function (City) {
City.GetCurrentPopulation = function (req) {
var population;
City.app.models.Pupulation.find({where{id:req.id}}, //This line //gives me an error that cannot read property 'find' of undefined
function(req.res){
population=res.population;
});
response='Population for ' +req.cname ' is' +population;
req(null, response);
};
City.remoteMethod(
'GetCurrentPopulation', {
http: {
path: '/GetCurrentPopulation',
verb: 'GetCurrentPopulation'
},
returns: {
arg: 'startdate',
type: 'string'
}
}
);Run Code Online (Sandbox Code Playgroud)
有一个模型城市,我想访问另一个模型,如"population.find(一些过滤器)"如何做到这一点?
我有一个用城市模型编写的远程方法.我试图访问人口记录的地方
var countryp = population.find(其中{id:4});
var currentpopulation = countryp.Totalpopulation;
它给出了一个错误population.find不是一个函数.
请建议如何做到这一点.
只有在城市和人口模型之间定义了一些关系时,City.app.models.Population才有效.否则它不会那样工作.如果与其他模型没有关系.您需要使用以获取对app对象的引用
试试这样:
var app = require('../../server/server');
module.exports = function (City) {
var Population = app.models.Population;
City.GetCurrentPopulation = function(req) {
Population.find({where{id:req.id}}, function (err) {
if (err) {
return console.log(err);
} else {
// do something here
});
}
Run Code Online (Sandbox Code Playgroud)
你可以参考这里的文档https://loopback.io/doc/en/lb3/Working-with-LoopBack-objects.html#using-model-objects
| 归档时间: |
|
| 查看次数: |
4886 次 |
| 最近记录: |