Loopback - 如何使用loopback扩展api

时间:2017-10-06 20:27:36

标签: loopbackjs

我想使用loopback扩展我的api。我已阅读文档

   'use strict';

module.exports = function(Meetups,pusher) {

  Meetups.status = function(cb) {
      var currentDate = new Date();
      var currentHour = currentDate.getHours();
      var OPEN_HOUR = 6;
      var CLOSE_HOUR = 20;
      console.log('Current hour is %d', currentHour);
      var response;
      if (currentHour >= OPEN_HOUR && currentHour < CLOSE_HOUR) {
        response = 'We are open yeah!!! for business.';
      } else {
        response = 'Sorry, we are closed. Open daily from 6am to 8pm.';
      }
      cb(null, response);
    };
    Meetups.remoteMethod(
      'status', {
        http: {
          path: '/status',
          verb: 'get'
        },
        returns: {
          arg: 'status',
          type: 'string'
        }
      }
    );
    Meetups.pusher = function(cb) {

        if (2>1) {
          response = 'sending something';
        } else {
          response = 'mont blanc';
        }
        cb(null, response);
      };
      Meetups.remoteMethod(
        'pusher', {
          http: {
            path: '/pusher',
            verb: 'get'
          },
          returns: {
            arg: 'pusher',
            type: 'string'
          }
        }
      );

};

首先,我添加/状态路线,它工作正常。但是,当我试图添加/推动。它只是没有用。我收到了错误

   {
  "error": {
    "statusCode": 500,
    "name": "ReferenceError",
    "message": "response is not defined",
    "stack": "ReferenceError: response is not defined\n    at Function.Meetups.pusher (/Users/ankursharma/Documents/projects/meetupz/common/models/meetups.js:34:20)\n    at SharedMethod.invoke (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/lib/shared-method.js:270:25)\n    at HttpContext.invoke (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/lib/http-context.js:297:12)\n    at phaseInvoke (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/lib/remote-objects.js:677:9)\n    at runHandler (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/node_modules/loopback-phase/lib/phase.js:135:5)\n    at iterate (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)\n    at Object.async.eachSeries (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:162:9)\n    at runHandlers (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/node_modules/loopback-phase/lib/phase.js:144:13)\n    at iterate (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)\n    at /Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:157:25\n    at /Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:154:25\n    at execStack (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/lib/remote-objects.js:522:7)\n    at RemoteObjects.execHooks (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/lib/remote-objects.js:526:10)\n    at phaseBeforeInvoke (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/lib/remote-objects.js:673:10)\n    at runHandler (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/node_modules/loopback-phase/lib/phase.js:135:5)\n    at iterate (/Users/ankursharma/Documents/projects/meetupz/node_modules/strong-remoting/node_modules/loopback-phase/node_modules/async/lib/async.js:146:13)"
  }
}

我很确定,这是一个非常小的错误。我是loopback的初学者,并试图在我的项目中实现loopback。

3 个答案:

答案 0 :(得分:1)

在示例中,他们将响应定义为该远程方法的局部变量,但事实并非如此。其次,(Meetups,pusher)你不需要在这里输出推杆。您正在添加到Meetups。

答案 1 :(得分:0)

您必须在Pusher远程方法中声明响应。 不声明响应的另一种方法是,仅返回值。

示例:

Meetups.pusher = function(cb) {
    if (2>1) {
        return 'sending something';
    } else {
        return 'mont blanc';
    }
};

答案 2 :(得分:0)

定义变量并返回变量,或者您可以直接在if和else中调用cb

Meetups.pusher = function(cb) {
    if (2>1) {
        cb(null,'sending something');
    } else {
        cb(null, 'mont blanc');
    }
};