I have an object in my Node.js route (result), and I want to split that into two separate objects and return them both. However, sometimes the second object is returned empty and to fix that I want to make the function that splits it asynchronous. For some reason its not working though, what am I doing wrong? The function is separateResult().
EDIT: The problem seems to be in my createResponse(rows) function. My SQL query always returns 6 rows but my createResponse(rows) function sometimes returns an object with 5 key-value pairs instead of 6.
function createResponse(rows) {
var response = {}
var random = 0
for (let i = 0; i < 6; i++) {
var random = Math.floor(Math.random() * 101)
response[random] = rows[i].user_name
}
return response
}
separateResultfunction. Remove theasynckeyword and call the function like you would call any other synchronous function. Also don'tawaitthe call to this function.resultobject and observe its value whenseparateResultfunction returns unexpected value.resultare you passing toseparateResultfunction as a parameter?