I'm trying to make this brief. Hopefully, it isn't so brief it makes no sense. I need to read a list of identifiers from an API that will be used to subsequently 'GET' the JSON files associated with the keys in the list. The list array is stored in another JSON file at the same endpoint with an identifier of 'keys.json' In order to prevent the list from being processed twice, I want to immediately write an empty array back to 'keys.json' upon retrieving it.
Here is the successful function that 'GETS' the list.
const getJsonKeys = async () => {
const options = {
method: 'GET',
uri: baseURL + keysID,
headers: { 'User-Agent': 'Request-Promise' },
json: true // Automatically parses the JSON string in the response
};
return (await rpn(options)).keys;
};
Here is the unsuccessful 'POST' that I try to write with:
const postEmptyJsonKeys = async () => {
const options = {
method: 'POST',
uri: baseURL + keysID,
body: {
keys: []
},
json: true // Automatically stringifies the body to JSON
};
return (await rpn(options)).req.Request.body;
};
Here is the block that calls them both:
module.exports = (rtProcess) => {
rtProcess.get('/process', async (req, res, next) => {
const jsonKeysList = await (getJsonKeys());
console.log("Retrieved Keys", jsonKeysList);
try {
const req = await (postEmptyJsonKeys());
console.log("Wrote", req.Request.body);
} catch(err) {
console.log(err.statusCode, err.error);
console.log(err);
}
//
// more code here
//
jsonKeysList.forEach(courseID => {
//
// more code here
//
});
res.render("process");
}); // end of process route
}; // end of module exports
I have tried everything I know to do to ferret out the answer in the various docs around but I can find nothing that tells me why the catch block is taken, rather than getting a successful try.
BTW the error.status code is a 404.
The error is a string that looks like HTML, which is also a mystery to me since I am trying to POST a simple:
{
keys: []
}