环回-返回功能而不是值 [英] Loopback - getting function back instead of values

查看:60
本文介绍了环回-返回功能而不是值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于某些端点,在调用时我会得到一个函数,而不是实际值(末尾的代码).看起来它仅在我的Patient模型中的嵌套端点中出现.

For some endpoints I get a function back when called instead of the actual values (code at the end). It looks like that it only appears for nested endpoints through my Patient model.

例如localhost:3000/api/Patients/{id}/MeasDescPatRels

但这很好用:localhost:3000/api/Patients/{id}/MeasuredDataPoints

在我的web应用程序中,这实际上不是问题,显然返回的函数只是由JS调用的,并为我提供了正确的数据.但是,我有一个Android应用程序调用了完全相同的端点.

In my webapp it is not really a problem, apparently the returned function is just called by JS and gives me the correct data. However I have an android app calling the exact same endpoints.

我也不确定何时确切开始这种行为.有时在创建患者模型之后就可以使用,但是有时它可以工作数小时甚至数天.

I'm also not sure when exactly this behaviour starts. Sometimes right after creating a patient model but sometimes it works for hours, even days.

返回的功能:

function (condOrRefresh, options, cb) {
    if (arguments.length === 0) {
      if (typeof f.value === 'function') {
        return f.value(self);
      } else if (self.__cachedRelations) {
        return self.__cachedRelations[name];
      }
    } else {
      if (typeof condOrRefresh === 'function' &&
          options === undefined && cb === undefined) {
        // customer.orders(cb)
        cb = condOrRefresh;
        options = {};
        condOrRefresh = undefined;
      } else if (typeof options === 'function' && cb === undefined) {
        // customer.orders(condOrRefresh, cb);
        cb = options;
        options = {};
      }
      options = options || {};
      // Check if there is a through model
      // see https://github.com/strongloop/loopback/issues/1076
      if (f._scope.collect &&
        condOrRefresh !== null && typeof condOrRefresh === 'object') {
        //extract the paging filters to the through model
        ['limit', 'offset', 'skip', 'order'].forEach(function(pagerFilter) {
          if (typeof(condOrRefresh[pagerFilter]) !== 'undefined') {
            f._scope[pagerFilter] = condOrRefresh[pagerFilter];
            delete condOrRefresh[pagerFilter];
          }
        });
        // Adjust the include so that the condition will be applied to
        // the target model
        f._scope.include = {
          relation: f._scope.collect,
          scope: condOrRefresh,
        };
        condOrRefresh = {};
      }
      return definition.related(self, f._scope, condOrRefresh, options, cb);
    }
  }

MeasDescPatRels模型(不起作用):

MeasDescPatRels model (not working):

{
    "name": "MeasDescPatRel",
    "base": "PersistedModel",
    "strict": false,
    "idInjection": false,
    "options": {
      "validateUpsert": true
    },
    "properties": {
      "reminderData": {
        "type": "object"
      },
      "alarmData": {
        "type": "object"
      }
    },
    "validations": [],
    "relations": {
      "patient": {
        "type": "belongsTo",
        "model": "Patient",
        "foreignKey": "patientId"
      },
      "measurementDescription": {
        "type": "belongsTo",
        "model": "MeasurementDescription",
        "foreignKey": ""
      }
    },
    "acls": [
      {
        "accessType": "*",
        "principalType": "ROLE",
        "principalId": "$everyone",
        "permission": "ALLOW"
      },
      {
        "accessType": "WRITE",
        "principalType": "ROLE",
        "principalId": "$everyone",
        "permission": "ALLOW"
      }
    ],
    "methods": {}
  }

HomeGateway模型(不起作用):

HomeGateway model (not working):

{
  "name": "HomeGateWay",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "model": {
      "type": "string",
      "required": true
    },
    "version": {
      "type": "string",
      "required": true
    },
    "onlineStatus": {
      "type": "boolean",
      "required": true
    },
    "macAdress": {
      "type": "string",
      "id": true,
      "required": true
    }
  },
  "validations": [],
  "relations": {
    "patient": {
      "type": "belongsTo",
      "model": "Patient",
      "foreignKey": ""
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": {}
}

MeasuredDataPoint模型(有效):

MeasuredDataPoint model (working):

    {
  "name": "MeasuredDataPoint",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "created": {
      "type": "date",
      "required": false
    },
    "timestamp": {
      "type": "date",
      "required": false
    },
    "lastUpdated": {
      "type": "date",
      "required": false
    }
  },
  "validations": [],
  "relations": {
    "measurementDescription": {
      "type": "belongsTo",
      "model": "MeasurementDescription",
      "foreignKey": ""
    },
    "patient": {
      "type": "belongsTo",
      "model": "Patient",
      "foreignKey": "patientId"
    }
  },
  "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW"
    }
  ],
  "methods": {}
}

推荐答案

我重现了此错误:

SomeModel.find({ where: { userId: userId }, include : ['otherModel'] }, (err, response) => {
    var someArray = response;
    for (var x in someArray) {
      var otherModel = someArray[x].otherModel;
      console.log(otherModel);
    }
 });

otherModel的控制台输出为:

The console output of otherModel is :

function (condOrRefresh, options, cb) {
        if (arguments.length === 0) {
          if (typeof f.value === 'function') {
            return f.value(self);
          } else if (self.__cachedRelations) {
            return self.__cachedRelations[name];
          }
        } else {
          const condOrRefreshIsCallBack = typeof condOrRefresh === 'function' &&
            options === undefined &&
            cb === undefined;
          if (condOrRefreshIsCallBack) {
            // customer.orders(cb)
            cb = condOrRefresh;
            options = {};
            condOrRefresh = undefined;
          } else if (typeof options === 'function' && cb === undefined) {
            // customer.orders(condOrRefresh, cb);
            cb = options;
            options = {};
          }
          options = options || {};
          // Check if there is a through model
          // see https://github.com/strongloop/loopback/issues/1076
          if (f._scope.collect &&
            condOrRefresh !== null && typeof condOrRefresh === 'object') {
            f._scope.include = {
              relation: f._scope.collect,
              scope: condOrRefresh,
            };
            condOrRefresh = {};
          }
          return definition.related(self, f._scope, condOrRefresh, options, cb);
        }
      }

由于otherModel的输出是一个函数.我尝试用以下方式调用它: var otherModel = batchArray[x].otherModel()作为功能而不是var otherModel = batchArray[x].otherModel.它会提供所需的输出.

Since the output of otherModel is a function. I tried calling it with : var otherModel = batchArray[x].otherModel() as a function instead of var otherModel = batchArray[x].otherModel. It gives the desired output.

此行为说明,include过滤器返回模型的函数,该函数将被执行并作为对象传递给前端,但在后端则需要将其称为函数.

This behaviour explains that the include filter returns a function of the model which gets executed and passes as an object to the frontend but in the backend it needs to be called as a function.

我仍在尝试弄清楚它如何将所需的对象返回到前端以及将函数返回到后端.任何线索都将有所帮助.

I am still trying to figure out how it returns the desired object to frontend and a function to backend. Any leads would be helpful.

这篇关于环回-返回功能而不是值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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