Loopback - 如何使用loopback扩展api

时间:2020-12-12 23:15:33

I want to extend my api using loopback . I have read the documentation

我想用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'
          }
        }
      );

};

First, I added /status route and it worked fine. But, when i tried to add /pusher . It just didnt work. I am getting an error

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

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

I am pretty sure, its a very small mistake. I am beginner in loopback and trying to implement loopback in my project.

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

2 个解决方案

#1


1  

In the example they define response as a local variable to that remote method, you did not. Secondly, (Meetups,pusher) you do not need to export pusher here. You are adding to Meetups.

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

#2


0  

You have to declare response in your pusher remote method. An alternative way without declaring response is, Simply returning the value.

您必须在推送远程方法中声明响应。没有声明响应的另一种方法是,简单地返回值。

Example:

例:

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

#1


1  

In the example they define response as a local variable to that remote method, you did not. Secondly, (Meetups,pusher) you do not need to export pusher here. You are adding to Meetups.

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

#2


0  

You have to declare response in your pusher remote method. An alternative way without declaring response is, Simply returning the value.

您必须在推送远程方法中声明响应。没有声明响应的另一种方法是,简单地返回值。

Example:

例:

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