-1

I am running a webhook to pull and process data from seller.tools api.

I am trying to call their api with a fetch using node-fetch. When I am testing it in the cloud9 ide, to transfer it to aws-lambda, anyway I try and grab the result it comes as undefined.

In fact, if I try to call console.log in the .then, it doesn't even show up. Also, it doesn't even throw an error except when I try and access the result further on.

...
var order, status;
let url='https://data.seller.tools/api/v1/orders/'+orderid;
let options={
  headers:{
    'Authorization':apikey
  }
};

try{
  fetch(url,options)
  .then(res => {status=res.status;order=res.json();return res;})
  .then(res => console.log(res))
  .catch(err => console.error("Err"+err));
}catch(err){console.log(err)}
console.log("order: "+order);
console.log("status: "+status);
console.log(order.order_id);
...

Result:

order: undefined 
status: undefined
{"errorType":"TypeError","errorMessage":"Cannot read property 'order_id' of undefined"}

I've used the same url and headers, in talend api tester extension, it works as it should. but in my code, running the test in cloud9 ide, it doesn't. What is going on?

Robert Fiola
  • 1
  • 1
  • 3
  • My guess is the format of the authorization key. Note that 3xx-5xx responses are NOT exceptions, therefore you may well be getting something back, but no response because the authorisation failed. On the node-fetch npm site, there's a set of instructions on how to check status errors. "Handling client and server errors" - try that out. And all of the last console.log statement will be undefined because you're making an async call. You'll hit the console.log statements before fetch has returned anything. – Tom Mar 11 '20 at 18:15
  • So how would I make my script wait for the response before continuing? – Robert Fiola Mar 11 '20 at 19:22
  • Welcome to the world of async Javascript! Either await the result in an async function or you need to start chaining promise calls. What was the status and statusText? – Tom Mar 12 '20 at 07:57
  • I was thinking I was chaining promise calls by doing `.then`. I need this function to work synchronously so that I can continue with the script. Everything depends on this fetch. – Robert Fiola Mar 12 '20 at 13:23

2 Answers2

0

You should be logging inside then method since the call is async and logging will happen before the call has resolved.


var order, status;
let url = 'https://data.seller.tools/api/v1/orders/' + orderid;
let options = {
  headers: {
    'Authorization': apikey
  }
};

try {
  fetch(url, options)
    .then(res => {
      status = res.status;
      order = res.json();
      console.log("order: " + order);
      console.log("status: " + status);
      console.log(order.order_id);

      return res;
    })
    .catch(err => console.error("Err" + err));
} catch (err) {
  console.log(err)
}

Ayush
  • 131
  • 3
  • the logging isn't as important (although it tells me where my variables are at).What I am trying to do is access the response in the rest of my script. I want to wait to receive the result before continuing. – Robert Fiola Mar 11 '20 at 20:18
0

I managed to get a response from fetch by making my wrapping function async and using await to make sure the fetch happens synchronously.

async function loadJson(url,options) { 
  let response = await fetch(url,options);
  if (response.status == 200) {
    let json = await response.json();
    return {'status':response.status,'json':json};
  }

  throw new Error(response.status);
}
async function getOrder(orderid) {
//...
var order, status;
let url='https://data.seller.tools/api/v1/orders/'+orderid;
let options={
  headers:{
    'Authorization':apikey
  }
};

try{
        var response;

        await loadJson(url,options)
        .then(res => response=res)
        .catch(err => console.log(err)); // Error: 404
        var json = response.json;
        console.log('Order:' + JSON.stringify(json));
        var status=response.status;
        console.log('Status:'+status);
    }catch(err){
        console.log(err);
        return err;
    }
//...
}

Log:

Order: {/*good order info here*/}
Status: 200

unfortunately, this means that there is a ripple effect that now the wrapper function is working asynchronously and returns a promise. The next step is how to stop this ripple.

Robert Fiola
  • 1
  • 1
  • 3
  • From what I found, there is no way to stop this 'async/await cycle'. If anyone has a solution, I would appreciate the help. – Robert Fiola Mar 17 '20 at 13:55