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.

Vincent
  • 852
  • 6
  • 29
  • 67
  • 1
    I don't understand what you are trying to do. Please add some code samples demonstrating what you already have and provide more details on what you are trying to accomplish, e.g. how should the client-to-another-server workflow look like. – Miroslav Bajtoš Jun 04 '14 at 07:33

1 Answers1

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

This code is exposing your Count method in your loopback REST API server. The http.path options is a path where the method is available. In this case, it would be usually something like /count.

Few more things to fix:

  • model classes can be accessed via app.models, not app.model.
  • since you defined Users.count, the argument to loopback.remoteMethod should be Users.count, not Product.count.

Note that the count method is already provided for you by LoopBack.

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

LoopBack does not provide support for this out of the box. However, you can implement a hook that will post the result to another server:

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();
  });
});

See Remote methods and hooks in LoopBack documentation for more details.

Miroslav Bajtoš
  • 10,667
  • 1
  • 41
  • 99