loopback remote方法不接受参数或给出错误

时间:2016-07-23 05:50:20

标签: loopbackjs strongloop

我使用

生成了远程方法
  

slc loopback:remote-method

我尝试从资源管理器访问此方法,它给状态400提供消息" phoneNumber是必需的arg"

当我尝试使用邮递员时,它会给出状态500并显示消息内部服务器错误。我尝试以表格形式发送帖子数据,x-www-form-urlencoded编码和行,它给出相同的结果。

服务器端错误:

Unhandled error for request POST /otp/getOTP: TypeError: Cannot read property 'modelName' of null
at convertToBasicRemotingType (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:390:16)
at /Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:544:20
at Array.filter (native)
at Function.SharedMethod.toResult (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:534:21)
at callback (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:249:31)
at Function.Onetimepassword.getOTP (/Users/manish/Documents/workspace-node/carbuk-services/common/models/one-time-password.js:14:4)
at SharedMethod.invoke (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/strong-remoting/lib/shared-method.js:263:25)
at HttpContext.invoke (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/strong-remoting/lib/http-context.js:387:12)
at phaseInvoke (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/strong-remoting/lib/remote-objects.js:644:9)
at runHandler (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/loopback-phase/lib/phase.js:135:5)
at iterate (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/async/lib/async.js:146:13)
at Object.async.eachSeries (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/async/lib/async.js:162:9)
at runHandlers (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/loopback-phase/lib/phase.js:144:13)
at iterate (/Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/async/lib/async.js:146:13)
at /Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/async/lib/async.js:157:25
at /Users/manish/Documents/workspace-node/carbuk-services/node_modules/loopback/node_modules/async/lib/async.js:154:25

模型json看起来像这样

{   
    "name": "OneTimePassword",   
    "plural": "otp",   
    "base": "PersistedModel",   
    "idInjection": true,   
    "options": {
       "validateUpsert": true
    },   
    "properties": {
       "phoneNumber": {
          "type": "number",
          "required": true
       },
    "otpNumber": {
      "type": "number",
      "required": true
    },
    "resendCounter": {
      "type": "number",
      "default": "0"
    },
    "createdDate": {
      "type": "date"
    }   },   "validations": [],   "relations": {},   "acls": [
    {
      "accessType": "*",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "DENY"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "deleteById"
    },
    {
      "accessType": "EXECUTE",
      "principalType": "ROLE",
      "principalId": "$everyone",
      "permission": "ALLOW",
      "property": "getOTP"
    }   ],   "methods": {
    "getOTP": {
      "accepts": [
        {
          "arg": "phoneNumber",
          "type": "number",
          "required": true,
          "description": "phone number",
          "http": {
            "source": "form"
          }
        }
      ],
      "returns": [
        {
          "arg": "oneTimePassword",
          "type": null,
          "root": true,
          "description": "otp"
        }
      ],
      "description": "generate otp and send sms",
      "http": [
        {
          "path": "/getOTP",
          "verb": "post"
        }
      ]
    }   } }

模型js:

    module.exports = function(Onetimepassword) {
    /**
     * generate otp and send sms
     * @param {number} phoneNumber phone number
     * @param {Function(Error, )} callback
     */
    Onetimepassword.getOTP = function(phoneNumber, callback) {
      var oneTimePassword = {};
      oneTimePassword.phoneNumber = phoneNumber;
      // TODO
      // logic will come here
      // 
      callback(null, oneTimePassword);
    };
   };

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

你有一个错字。

在行oneTimePassword.phoneNumber = phoneNumber;

,您需要设置为optNumber

答案 1 :(得分:0)

我找到了解决方案。

  

在model.json中,Remote方法的返回类型为null。我将其更改为对象类型" OneTimePassword"

但我仍然无法从资源管理器中点击远程方法。有一个错误issue#440

使用邮递员,请求正常工作