首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Loopback -取回函数而不是值

Loopback -取回函数而不是值
EN

Stack Overflow用户
提问于 2017-04-25 19:31:57
回答 1查看 890关注 0票数 3

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

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

然而,这样做很好:localhost:3000/api/Patients/{id}/MeasuredDataPoints

在我的webapp中,这并不是真正的问题,显然,返回的函数只是由JS调用,并为我提供了正确的数据。然而,我有一个android应用程序调用完全相同的端点。

编辑:我也不确定这种行为是什么时候开始的。有时就在创建患者模型之后,但有时它会工作几个小时,甚至几天。

返回函数:

代码语言:javascript
复制
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模型(不工作):

代码语言:javascript
复制
{
    "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模型(不工作):

代码语言:javascript
复制
{
  "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模型(工作中):

代码语言:javascript
复制
    {
  "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": {}
}
EN

回答 1

Stack Overflow用户

发布于 2018-07-08 03:05:07

我重现了这个错误:

代码语言:javascript
复制
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的控制台输出为:

代码语言:javascript
复制
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来调用它。它会给出所需的输出。

此行为解释了include过滤器返回模型的一个函数,该函数被执行并作为对象传递到前端,但在后端它需要作为一个函数调用。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43609499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档