4

When I try to post an instance to loopback from within nodejs using this code I don't get any errors but I don't get any data posted either?

//NPM Package (request)
var request = require('request'); 

// Address of Loopback API on the same server
var api = "http://localhost:3000/api/devices"; 

//JSON Construction
var deviceInstance = {
     "manufacturer": "manufacturer",
     "model": "model"
   //etc
}

// NPM (request)
request({
   url: api,
   method: "POST",
   headers: {"Accept: application/json"},
   json: true,
   body: deviceInstance
}, function (error, response, body) {
      if(error) {
        console.log('error: '+ error);
      } else {
        console.log('document saved to api')
        console.log(body);
        console.log(response);
      }
});

process.exit();

I'm not getting any response or erros from the server which is the same machine. If I try the same call in postman (windows app) it actually creates an instance in the API so why isn't my local node connecting to the API?

mitchelangelo
  • 851
  • 4
  • 16
  • 42

3 Answers3

1

Why process.exit() ?

Calling process.exit() will force the process to exit as quickly as possible even if there are still asynchronous operations pending.

  • [Source](https://nodejs.org/api/process.html#process_process_exit_code). You can resolve this by moving `process.exit()` to inside the `request` callback – Jimmy Feb 08 '19 at 19:50
  • This is a script to insert thousands of items into loopback and once it's done I want the node to stop. I can't move process.exit into the callback because this code is in a loop. – mitchelangelo Feb 09 '19 at 06:07
  • But I can't get this one test item to even post from nodejs – mitchelangelo Feb 09 '19 at 06:13
  • Here's the weird thing the code kinda works with postman but not from within nodejs, with postman I get a post in loopback but I don't see the manufacturer or model data just a new Id in loopback api – mitchelangelo Feb 09 '19 at 06:27
1

What happens and why

The behaviour you are seeing is because of the asynchronous nature of Javascript.

Your code, from top to bottom, starts the POST request, and then calls process.exit() before the request is done, which gives the behaviour you are seeing and "breaks" your code.

From there you have two solutions:

Calling process.exit() in the callback of the request

//NPM Package (request)
var request = require('request'); 

// Address of Loopback API on the same server
var api = "http://localhost:3000/api/devices"; 

//JSON Construction
var deviceInstance = {
     "manufacturer": "manufacturer",
     "model": "model"
   //etc
}

// NPM (request)
request({
   url: api,
   method: "POST",
   headers: {"Accept: application/json"},
   json: true,
   body: deviceInstance
}, function (error, response, body) {
      if(error) {
        console.log('error: '+ error);
      } else {
        console.log('document saved to api')
        console.log(body);
        console.log(response);
      }
      //request is done, we can safely exit
      process.exit();
});

Calling the exit() function in the callback of your request will effectively assure you that the POST request is done and you can exit safely.

Removing process.exit() altogether

Fact is, you didn't need to exit manually: any Node process exits on its own once the event loop is empty. In other words, once no further task is scheduled for the process, node exits the process on its own.

You can find more information about this in the official documentation: https://nodejs.org/api/process.html#process_event_exit

Azami
  • 2,122
  • 1
  • 12
  • 22
0

request needs a callback :

request({
  url: api + "Devices",
  method: "POST",
  headers: "Accept: application/json",
  json: true,
  body: JSONParent
}, (err, result, body) => {
  // do your stuffs with the results
});
dun32
  • 708
  • 6
  • 9
  • same problem no response from the server I've updated my question to show how I am doing error reporting. currently nothing comes back after the request even though I specify to console.log the response, body and any errors. – mitchelangelo Feb 08 '19 at 19:35