【问题标题】:Loopback - getting function back instead of valuesLoopback - 获取函数而不是值
【发布时间】:2018-07-08 03:05:06
【问题描述】:

对于某些端点,我在调用时会返回一个函数,而不是实际值(最后的代码)。看起来它只通过我的 Patient 模型出现在嵌套端点中。

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

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

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

编辑:我也不确定这种行为何时开始。有时会在创建患者模型后立即运行,但有时会持续数小时甚至数天。

返回函数:

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 模型(不工作):

{
    "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 模型(不工作):

{
  "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 模型(工作):

    {
  "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": {}
}

【问题讨论】:

  • 它仍然是旧的......这可能是this 的副本。使用 toJSON() 将响应转换为 JSON 对象是那里公认的答案。我也会接受@Rohan 的回答

标签: javascript node.js loopbackjs loopback


【解决方案1】:

我重现了这个错误:

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 的控制台输出为:

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。它提供了所需的输出。

此行为说明包含过滤器返回模型的函数,该函数被执行并作为对象传递给前端,但在后端需要作为函数调用。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-10-07
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2016-08-09
    • 1970-01-01
    • 2012-08-10
    相关资源
    最近更新 更多