如何从一个持久模型的远程方法访问另一个持久环回模型的数据?

How to access data of another persisted model of loopback from remote method of one persisted model?

'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'
      }
    }
  );

有一个模型城市,我想访问另一个模型,例如 "population.find(some filters)" 如何操作?

我有一个用城市模型编写的远程方法。我在哪里尝试访问人口记录

var countryp=population.find(where{id:4});

var currentpopulation=countryp.Totalpopulation;

报错 population.find is not a function.

请建议执行此操作的方法。

City.app.models.Population 仅在您定义了城市与人口模型之间的某种关系时才有效。否则它不会那样工作。如果与其他模型没有关系。您需要使用

获取对应用程序对象的引用

这样试试:

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 
    });
}

您可以参考这里的文档https://loopback.io/doc/en/lb3/Working-with-LoopBack-objects.html#using-model-objects