如何在一个模型中使用远程方法从另一个模型返回信息? [英] How do I use a remote method in one model to return info from another model?

查看:80
本文介绍了如何在一个模型中使用远程方法从另一个模型返回信息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经设置了一些非常简单的方法来学习如何使用Loopback.

So I've set up something really simple to learn how to use Loopback.

模型如下:

Person 
   - based on built in User model
food_pref
   typeId (number)
   personId (number)
food_type
   type (string)

关系:

Person has many food_prefs (foreign key: personId)
food_pref belongs to Person (foreign key: personId)
food_pref belongs to food_type (foreign key: typeId)

创建了一个自动生成的方法,该方法根据Person的ID返回food_prefs.

An auto-generated method gets created that returns the food_prefs based on the id of the Person.

People/{id}/foodPrefs

这将返回:

[
  {
    "typeId": 0,
    "personId": 0,
    "id": 0
  }
]

我想做的是添加一个单独的名为"getPrefs"的远程方法,该方法根据food_pref中的typeId返回food_type下的类型名称.

What I want to do is add a separate remote method called "getPrefs" that returns the name of the type under food_type based on the typeId in food_pref.

因此,假设food_types中的typeId为1且id 1为Italian Food,则远程方法将返回以下内容:

So let's say typeId is 1 and id 1 in food_types is Italian Food then the remote method would return this:

{
  "type": "Italian Food"
}

有人告诉我要使用Person.js并在这些内容上添加一些内容,但是我对include语句以及括号内的内容感到困惑.通常它会崩溃并显示错误消息:错误:未为Person模型定义关系"food_pref",请参阅下面的建议:

I was told to use Person.js and add something along these lines but I'm really confused about the include statement as well as what to do inside the brackets. Often it crashes with an error saying: Error: Relation "food_pref" is not defined for Person model, see what they recommended below:

module.exports = function(Person) {
 Person.getPrefs = function(personId, cb) {
 Person.findById(personId, { include: { food_pref: "food_type" } }, function(err, user) {
    if (err) throw err;
 });
 }

 Person.remoteMethod (

        'getPrefs',
        {
          http: {path: '/getPrefs', verb: 'get'},
          accepts: {arg: 'personId', type: 'number', http: { source: 'query' } },

          returns: {arg: 'type', type: 'string'}
        }
    );
};

我在这里做什么错了?

推荐答案

在定义个人远程方法时,根据 strongloop文档, strongloop自动提供一个回调,如果需要,该回调将返回数据. 参见下面的更新代码

According to strongloop documentation when you define a personal remote method, strongloop automatically provides a callback that will returns datas if needed. See below updated code

您要在此food_pref中包含food_pref关系以及food_type实现.将其放入您的getPrefs自定义方法中:

You want to include the food_pref relation as well as the food_type realation inside this food_pref. Get this into your getPrefs custom method:

Person.getPrefs = function(personId, cb) {
    Person.findById(personId, {
        include: [{
            relation: 'food_pref',
            scope: {
                include: {
                    relation: 'food_type'
                }}}
        ]},
        function(err, personFound) {
            if (err)
                console.log(err);
            else {
                cb(null, personFound)
            }
        });
};

它的作用:使用personId参数和cb参数(由Strongloop自动传递)调用您的个人方法.您的方法通过id查找合适的人,包括关系(以及食物类型的名称),然后在获取结果后,"Person.findById"内部的回调将调用获取数据的回调"cb"(此处personFound)

What it does: your personal method is called with the personId argument and a cb argument (automatically passed by strongloop !). Your method finds the right person by id, includes the relations (name of the type of food as well), then when the result has been fetched, the callback inside "Person.findById" calls the callback 'cb' with datas fetched (here personFound)

Person.remoteMethod(
    'getPrefs', {
        http: {path: '/:personId/getPrefs', verb: 'get'},
        accepts: [{arg: 'personId', type: 'number'}],
        returns: {arg: 'person', type: 'object'},
        description: ['a person object']
    }
);

然后,返回的对象应包含食物类型的名称. 确保在Person.json中包含正确的关系名称,依此类推.

Then the returned object should contain the name of the type of food. Make sure you include the right relation names you have in your Person.json and so on.

如果您只想要食物偏好的名称,请使用不同的方法遵循相同的想法:

If you just want the name of food pref, follow the same idea with different methods:

  • 只需将对象内部的字符串发送到自动回调参数'cb'(并修改注册以宣布方法发送的返回类型)

  • just send the string inside your object to the automatic callback argument 'cb' (and modify the registration to announce the kind of return your method sends)

直接在food_type表中搜索"where"条件(其中personId =您要寻找的人的ID)

search directly inside food_type table with a "where" condition (where personId = the id of the person you are looking for)

看看关于Strongloop文档的链接,因为它是有关远程方法的漂亮且详细的内容.

Take a look at the link at the link to strongloop doc as it is pretty and detailed regarding remote methods.

希望有帮助.

这篇关于如何在一个模型中使用远程方法从另一个模型返回信息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆