I was getting an issue in my React client side, so I reproduced the error using a simpler single file node program. In the react client, I'm not getting any data back. Using Postman, I'm able to do a "Get" and retrieve the JSON data.
Can someone help me to get this to run without hanging?
I recently did a similar function in the server side gettranslations so that I could call MongoDB from a function. (NodeJS ASync call of MongoClient GetData routine). I think I'm following the same pattern as my answer there.
I thought about using "await" but I'm not in an async function. And I didn't need "await' in the above example I just mentioned, since the function uses the ".then" pattern.
Partial Code:
const fetch = require("node-fetch");
function getDBLanguagesAndConvertToDataTable(fromLanguage, toLanguage) {
// TODO implement from/to Language in URL below
console.log("Function: " + getFormattedTime())
return fetch('http://localhost:3001/api/gettranslations/en-US/es-MX', {
method: 'GET',
headers: {
'Accept': 'application/json'
}
})
.then(function(data) {
// do something with the data
console.log("Status:", data.status)
console.log("data from DB:")
console.log(JSON.stringify(data))
console.log("data.body:")
console.log(JSON.stringify(data.json()))
// I call another function here to reshape the data,
// but even with that commented out, same issue.
return data.json()
})
.catch(function(err){
console.log("Error:" + err)
throw new Error(err)
})
}
console.log("------- Test with Fetch/Get/FromWebservice -------")
console.log(getFormattedTime())
var fromLanguage = 'en-US'
var toLanguage = 'es-MX'
getDBLanguagesAndConvertToDataTable(fromLanguage, toLanguage)
.then(function (dataTable) {
console.log(getFormattedTime, " Back from call fetch")
var strDataTable = JSON.stringify(dataTable, null, 3);
console.log("ReactTables(): Resulting DataTable=")
console.log(strDataTable)
console.log(getFormattedTime(), "The end of tests ")
})
I run "node test.js". It displays the following and hangs. I have to do CNTL-Break to end it.
------- Test with Fetch/Get/FromWebservice -------
2020-08-26-20-43-08
Function: 2020-08-26-20-43-08
Part 2 - after adding Catches
Revised code:
function getDBLanguagesAndConvertToDataTable(fromLanguage, toLanguage) {
// TODO implement from/to Language in URL below
console.log("Function: " + getFormattedTime())
return fetch('http://localhost:3001/api/gettranslations/en-US/es-MX', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
}
})
.then(function(data) {
// do something with the data
console.log("Status:", data.status)
console.log("data from Fetch:")
console.log(JSON.stringify(data))
console.log("data.json():")
console.log(JSON.stringify(data.json()))
//var tmpDataTable = convertMongoLanguagesToDataTable(data.json())
//return tmpDataTable
return data.json()
})
.catch(function(err){
console.log("Fetch Error:" + err)
throw new Error(err)
})
}
/*
console.log("------- Test with variable data-------------")
console.log(getFormattedTime())
dataTable = convertMongoLanguagesToDataTable(dbGetResults)
var strDataTable = JSON.stringify(dataTable, null, 3);
console.log("Resulting DataTable=")
console.log(strDataTable)
console.log("\n\n")
*/
console.log("------- Test with Fetch/Get/FromWebservice -------")
console.log(getFormattedTime())
var fromLanguage = 'en-US'
var toLanguage = 'es-MX'
getDBLanguagesAndConvertToDataTable(fromLanguage, toLanguage)
.then(function (dataTable) {
console.log(getFormattedTime, " Back from call fetch")
var strDataTable = JSON.stringify(dataTable, null, 3);
console.log("ReactTables(): Resulting DataTable=")
console.log(strDataTable)
console.log(getFormattedTime(), "The end of tests ")
.catch(function (err){
console.log("call function getDBLanguagesAndConvertToDataTable error:",
getFormattedTime(), "The end of tests ")
})
})
Output:
------- Test with Fetch/Get/FromWebservice -------
2020-8-26-21-31-5
Function: 2020-8-26-21-31-5
Status: 200
data from Fetch:
{"size":0,"timeout":0}
data.body:
{}
Fetch Error:TypeError: body used already for: http://localhost:3001/api/gettranslations/en-US/es-MX
(node:38732) UnhandledPromiseRejectionWarning: Error: TypeError: body used already for: http://localhost:3001/api/gettranslations/en-US/es-MX
at E:\GitHub\NealWalters\KyleShedCompanyPOC\shedco-backend\test_convert_languages_to_data_table.js:144:12
at processTicksAndRejections (internal/process/task_queues.js:97:5)
(node:38732) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 3)
(node:38732) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.