Hello everyone!
I ran into a problem that when calling the API, its response comes after the code was processed and I get undefined in the response.
For this, I have 2 files start.js and api.js.
In start.js I call api.js and wait for a response:
const api = require("./api"); const result = api.users(); console.log(result: ${result});
In api.js, I call the test api itself:
const request = require('request');
module.exports = {
users: function() {
request.post({
url: "https://reqres.in/api/register",
json: true,
body: {
"email": "eve.holt@reqres.in",
"password": "pistol"
},
headers: {
'User-Agent': 'request'
}
}, (err, res, data) => {
if (err) {
console.log('Error: ', err);
return err;
} else if (res.statusCode !== 200) {
console.log('Status: ', res.statusCode);
return `Status: ${res.statusCode}`;
} else {
console.log(`Success: ${JSON.stringify(data)}`);
return data;
}
});
},
Please tell me how to set up a call so that the command in start.js will work after the response from api.js?
console.log(result: ${result})
I am attaching my text file with information.