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



'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(一些过滤器("如何做到这一点?

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

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

VaR 当前人口=国家P。总人口;

它给出了一个错误填充。find 不是一个函数。

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

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

最新更新