0

Can someone explain how this works, I have tried passing the data from my own server and send it to mysql. but my next task is to pass the json object to another serve using http post method

here is the link: http://docs.strongloop.com/display/DOC/Remote+methods+and+hooks

i can't seem to understand where to put this sample codes and recode it.

i'm also trying to pass the data. i edited my app.js

here is what i added.

var Users = app.model.userRegistrations;
Users.count = function(fn) {
  var usercount = {
    count: 123456
  };
  var err = null;

  // callback with an error and the result
  fn(err, usercount);
}

loopback.remoteMethod(
  Product.count,
  {
    returns: {arg: 'count', type: 'object'},
    http: {path: 'http://192.168.45.85:90', verb: 'get'}
  }
);

but i got an error

events.js:72
        throw er; // Unhandled 'error' event
              ^
TypeError: Cannot set property 'count' of undefined
    at Object.<anonymous> (/home/tsuper/supertsuper/app.js:15:13)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Object.<anonymous> (/usr/local/lib/node_modules/strong-supervisor/bin/slr:27:19)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)

is it correct that i put this codes in the app.js?

my goal is . after sending a data from my loopback. the loopback will also pass it to another server post method json data.

4

1 回答 1

1
loopback.remoteMethod(
  Product.count,
  {
    returns: {arg: 'count', type: 'object'},
   http: {path: 'http://192.168.45.85:90', verb: 'get'}
  }
);

此代码Count在您的环回 REST API 服务器中公开您的方法。http.pathoptions 是该方法可用的路径。在这种情况下,它通常类似于/count.

还有几件事要解决:

  • 模型类可以通过访问app.models,而不是app.model
  • 既然你定义Users.count了,参数loopback.remoteMethod应该是Users.count,不是Product.count

请注意,countLoopBack 已经为您提供了该方法。

我的目标是。从我的环回发送数据后。环回还将它传递给另一个服务器 post 方法 json 数据。

LoopBack 不为​​此提供开箱即用的支持。但是,您可以实现一个挂钩,将结果发布到另一台服务器:

var request = require('request');
Users.afterRemote('count', function(ctx, unused, next) {
  request.post({
    url: 'http://192.168.45.85:90/',
    method: 'POST',
    json: ctx.result
  }, function(err, response) {
    if (err) console.error(err);
    next();
  });
});

有关详细信息,请参阅LoopBack 文档中的远程方法和挂钩

于 2014-06-04T14:11:34.797 回答