Thursday, 14 March 2019

Trying to get node-fetch to work in a callback function

So I have a function that queries a database and formats the result into json. This json then is passed into a post function, but when the function is called it consoles the json but doesn't send the data to shopify. The post function works fine when called by itself outside of the callback. I'm using node-fetch and tedious.Function with the callback - queryDatabase(qryString, (err, results) => { if(err) { console.log(err); } else { console.log('working'); postJson(testJson); } })Post function - function postJson(body) { fetch('https://example.com', { method: 'post', body: JSON.stringify(body), headers: { 'Content-Type': 'application/json' }, }) .then(res => res.json()) .then(console.log('*******************************')) .then(console.log(body)) .then(json => console.log(json)) .catch(err => console.error(err)); }Thanks

Submitted March 14, 2019 at 02:59PM by TcHnMcR

No comments:

Post a Comment