Loopback: Cannot read property 'apply' of undefined

Created on 14 Sep 2015  路  9Comments  路  Source: strongloop/loopback

I'm writing my own custom method and I have been getting this error that I could not resolve. The error is {
"error": {
"name": "TypeError",
"status": 500,
"message": "Cannot read property 'apply' of undefined",
"stack": "TypeError: Cannot read property 'apply' of undefined\n at SharedMethod.invoke (C:\Users\cfeyerabend\PortalApi\node_modules\loopback\node_modules\strong-remoting\lib\shared-method.js:248:24)\n at HttpContext.invoke (C:\Users\cfeyerabend\PortalApi\node_modules\loopback\node_modules\strong-remoting\lib\http-context.js:384:12)\n at C:\Users\cfeyerabend\PortalApi\node_modules\loopback\node_modules\strong-remoting\lib\remote-objects.js:620:11\n at execStack (C:\Users\cfeyerabend\PortalApi\node_modules\loopback\node_modules\strong-remoting\lib\remote-objects.js:460:7)\n at RemoteObjects.execHooks (C:\Users\cfeyerabend\PortalApi\node_modules\loopback\node_modules\strong-remoting\lib\remote-objects.js:464:10)\n at C:\Users\cfeyerabend\PortalApi\node_modules\loopback\node_modules\strong-remoting\lib\remote-objects.js:617:10\n at C:\Users\cfeyerabend\PortalApi\node_modules\loopback\lib\application.js:357:13\n at C:\Users\cfeyerabend\PortalApi\node_modules\loopback\lib\model.js:313:7\n at C:\Users\cfeyerabend\PortalApi\node_modules\loopback\common\models\acl.js:465:23\n at C:\Users\cfeyerabend\PortalApi\node_modules\loopback\node_modules\async\lib\async.js:251:17"
}
}

Most helpful comment

@jjhesk If you're using isStatic: false, you need to define your function at ModelName.prototype.functionName. Also, make sure you use function () {} syntax, not () => {}, as that will give you a new scope.

All 9 comments

How does your remote method definition look like?

heres what it looks like
module.exports = function(GetRcEscalation) {
GetRcEscalation.getEscalations = function(Id_Number, cb) {
var ds = GetRcEscalation.dataSource;
var sql = 'sp_GetRCEscalations';
ds.connector.execute(sql, Id_Number, function(err, excalations) {
if (err) console.error(erro);
cb(err, excalations);
});
};
GetRcEscalation.remoteMethod(
'GetEscalations',
{
http: { verb: 'get',path:'/getEscalations' },
description: "Gets Escalation by the current User",
accepts: { arg: 'Id_Number', type: 'number' },
return: { arg: 'data', type: ['GetRcEscalation'], root: true }
}
)
};

There are two issues:

  • The method name should be getEscalations instead of GetEscalations.
GetRcEscalation.remoteMethod(
  'getEscalations', {
    http: { verb: 'get',path:'/getEscalations' },
    description: "Gets Escalation by the current User",
    accepts: { arg: 'Id_Number', type: 'number' },
    return: { arg: 'data', type: ['GetRcEscalation'], root: true }
  });
  • The SQL execute takes an array of params:
ds.connector.execute(sql, [Id_Number], function(err, excalations) { ...}

with this version 2.29.1 there is still buggy from reading the json configuration from the model.
I have got 'Cannot read property 'apply' of undefined' from using isStatic: false and I have the extract same error Cannot read property 'apply' of undefined.

@raymondfeng

  "methods": {
    "insertimagemeta": {
      "isStatic": false,
      "description": "Update the artist head image information to the system",
      "http": {
        "path": "/insertimagemeta",
        "verb": "post"
      },
      "accepts": [
        {
          "arg": "data",
          "type": "String",
          "required": true,
          "description": "Required input doc",
          "http": {
            "source": "body"
          }
        }
      ],
      "returns": {
        "arg": "image_doc",
        "type": "Object",
        "root": true,
        "description": "The updated image from user object."
      }
    }
}

This issue seems to get resolved if "path": "/insert" is changed to "path": "/". Might be a temporary solution.

path of what to where?

Why was this issue closed? This is not resolved. I'm having the same error when trying to access a custom prototype method.
I'm using loopback 3.0.0-alpha.2 because alpha 3 had too many issues.

Here is my configuration:

Participant.remoteMethod(
    'prototype.startSurvey',
    {
        description: 'Initiates the survey for the specified participant.',
        returns: {type: 'Participant', root: true},
        http: {path:'/startSurvey', verb: 'post'}
    }
);

And this is the error I get:

Unhandled error for request POST /Participants/57bd9eaae7ea405a475a6634/startSurvey: TypeError: Cannot read property 'apply' of undefined
    at SharedMethod.invoke (/Users/federico/dev/apis/icor-api/node_modules/strong-remoting/lib/shared-method.js:263:24)
    at HttpContext.invoke (/Users/federico/dev/apis/icor-api/node_modules/strong-remoting/lib/http-context.js:387:12)
    at phaseInvoke (/Users/federico/dev/apis/icor-api/node_modules/strong-remoting/lib/remote-objects.js:648:9)
    at runHandler (/Users/federico/dev/apis/icor-api/node_modules/loopback-phase/lib/phase.js:135:5)
    at iterate (/Users/federico/dev/apis/icor-api/node_modules/async/lib/async.js:146:13)
    at Object.async.eachSeries (/Users/federico/dev/apis/icor-api/node_modules/async/lib/async.js:162:9)
    at runHandlers (/Users/federico/dev/apis/icor-api/node_modules/loopback-phase/lib/phase.js:144:13)
    at iterate (/Users/federico/dev/apis/icor-api/node_modules/async/lib/async.js:146:13)
    at /Users/federico/dev/apis/icor-api/node_modules/async/lib/async.js:157:25
    at /Users/federico/dev/apis/icor-api/node_modules/async/lib/async.js:154:25
    at execStack (/Users/federico/dev/apis/icor-api/node_modules/strong-remoting/lib/remote-objects.js:493:7)
    at RemoteObjects.execHooks (/Users/federico/dev/apis/icor-api/node_modules/strong-remoting/lib/remote-objects.js:497:10)
    at phaseBeforeInvoke (/Users/federico/dev/apis/icor-api/node_modules/strong-remoting/lib/remote-objects.js:644:10)
    at runHandler (/Users/federico/dev/apis/icor-api/node_modules/loopback-phase/lib/phase.js:135:5)
    at iterate (/Users/federico/dev/apis/icor-api/node_modules/async/lib/async.js:146:13)
    at /Users/federico/dev/apis/icor-api/node_modules/async/lib/async.js:157:25

Setting the path to '/' as @apasrini suggested doesn't work; the remote method needs to have a proper path.

@jjhesk If you're using isStatic: false, you need to define your function at ModelName.prototype.functionName. Also, make sure you use function () {} syntax, not () => {}, as that will give you a new scope.

@dudeofawesome That solved my problem! I was defining my function as Participant.startSurvey. Defining it as Participant.prototype.startSurvey worked. That isn't very clear in the documentation. Thanks!

Was this page helpful?
0 / 5 - 0 ratings