I have a function that is supposed to take in an array of objects from one function, process the objects in the array, and return a specified output. Below is the said function;
function extractData(newResult) {
try {
const data = [];
data.push(Object.keys(newResult[0]));
for (let i = 0; i < newResult.length; i++) {
data.push(Object.values(newResult[i]));
}
return data;
} catch (error) {}
}
async function exportToCSV() {
try {
// const { title, emailAddress } = req.body;
const spreadsheetId = await createSpreadsheet(
"test",
"xyz@gmail.com"
);
const result = await account.findAll();
const newResult = JSON.stringify(result, null, 2);
console.log(newResult);
const data = extractData(newResult);
console.log(data);
const response = await writeToSpreadsheet(spreadsheetId, data);
// console.log(response);
} catch (error) {}
}
The extractData(newResult) function is supposed to take in the input below that's being contained in the newResult variable in exportToCSV() function...
[
{
"id": 1,
"auth_id": "20S0KPNOIM",
"username": "azr1"
},
{
"id": 2,
"auth_id": "54P2EOKQ47",
"username": "azr2"
},
{
"id": 3,
"auth_id": "9LLV6I4ZWI",
"username": "azr3"
},
{
"id": 4,
"auth_id": "YHWE3HDLPQ",
"username": "azr4"
},
{
"id": 5,
"auth_id": "6DLH8A25XZ",
"username": "azr5"
}
]
...and give this output below;
[
[ 'id', 'auth_id', 'username' ],
[ 1, '20S0KPNOIM', 'azr1' ],
[ 2, '54P2EOKQ47', 'azr2' ],
[ 3, '9LLV6I4ZWI', 'azr3' ],
[ 4, 'YHWE3HDLPQ', 'azr4' ],
[ 5, '6DLH8A25XZ', 'azr5' ]
]
but instead, I'm getting this;
[
[ '0' ], [ '[' ], [ '\n' ], [ ' ' ], [ ' ' ], [ '{' ],
[ '\n' ], [ ' ' ], [ ' ' ], [ ' ' ], [ ' ' ], [ '"' ],
[ 'i' ], [ 'd' ], [ '"' ], [ ':' ], [ ' ' ], [ '1' ],
[ ',' ], [ '\n' ], [ ' ' ], [ ' ' ], [ ' ' ], [ ' ' ],
[ '"' ], [ 'a' ], [ 'u' ], [ 't' ], [ 'h' ], [ '_' ],
[ 'i' ], [ 'd' ], [ '"' ], [ ':' ], [ ' ' ], [ '"' ],
[ '2' ], [ '0' ], [ 'S' ], [ '0' ], [ 'K' ], [ 'P' ],
[ 'N' ], [ 'O' ], [ 'I' ], [ 'M' ], [ '"' ], [ ',' ],
[ '\n' ], [ ' ' ], [ ' ' ], [ ' ' ], [ ' ' ], [ '"' ],
[ 'u' ], [ 's' ], [ 'e' ], [ 'r' ], [ 'n' ], [ 'a' ],
[ 'm' ], [ 'e' ], [ '"' ], [ ':' ], [ ' ' ], [ '"' ],
[ 'a' ], [ 'z' ], [ 'r' ], [ '1' ], [ '"' ], [ '\n' ],
[ ' ' ], [ ' ' ], [ '}' ], [ ',' ], [ '\n' ], [ ' ' ],
[ ' ' ], [ '{' ], [ '\n' ], [ ' ' ], [ ' ' ], [ ' ' ],
[ ' ' ], [ '"' ], [ 'i' ], [ 'd' ], [ '"' ], [ ':' ],
[ ' ' ], [ '2' ], [ ',' ], [ '\n' ], [ ' ' ], [ ' ' ],
[ ' ' ], [ ' ' ], [ '"' ], [ 'a' ],
... 273 more items
]