I have the following data structure in an array from Neo4j.
DATA: [
{
"keys": [
"J"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 9,
"high": 0
},
"labels": [
"Journal"
],
"properties": {
"body": "123"
},
"elementId": "9"
}
],
"_fieldLookup": {
"J": 0
}
},
{
"keys": [
"J"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 6,
"high": 0
},
"labels": [
"Journal"
],
"properties": {
"name": "Journal 221204",
"body": "<p>Test!</p>",
"lastEdit": "221204_03:53:02 PM",
"createdOn": "221204_03:45:33 PM"
},
"elementId": "6"
}
],
"_fieldLookup": {
"J": 0
}
}
]
I am trying to loop over the array, add specific data to a new array (finalList), and return the new array with this code.
// Allow require
import { createRequire } from "module";
const require = createRequire(import.meta.url);
require('dotenv').config()
var neo4j = require('neo4j-driver')
let finalList = [];
export default async function Database(query) {
const Neo4jUser = process.env.Neo4jUser;
const Neo4jPass = process.env.Neo4jPass;
const uri = "";
try {
const driver = neo4j.driver(uri, neo4j.auth.basic(Neo4jUser, Neo4jPass));
let session = driver.session();
let result = await session.run(query);
let records = Object.values(result)[0];
console.log(Object.keys(records).length);
if (Object.keys(records).length === 1) {
let record = Object.values(records)[0];
let fields = record._fields;
let fields2 = fields[0];
let properties = fields2.properties;
console.log(properties);
return properties;
};
if (Object.keys(records).length >= 2) {
let count = Object.keys(records).length;
console.log(2);
console.log(count);
let get_properties = async (records, countTimes) => {
let record = Object.values(records)[countTimes];
let fields = record._fields;
let fields2 = fields[0];
let properties = fields2.properties;
console.log(properties);
return properties;
};
let countTimes = 0;
while (countTimes < count) {
let node = get_properties(records, countTimes);
finalList.concat(node.then());
countTimes
};
console.log(finalList);
console.log(`DATA: ${records}`);
return finalList
};
}
catch (err) {
if (err.name == "Neo4jError") {
node = "No Database found"
return node;
}
else {
console.log(err);
}
};
};
Every time I run the code, finalList is returned empty.
How can I add the appropriate data to an array and have it returned inside the function?
I understand that I am having a problem with the scope of finalArray inside the function, as I am not able to have finalList.concat "save" the data, but I am unable to find a solution to my problem after my research.
Also, can anyone suggest a more efficient way of sorting the data than what I have above?
Any help is appreciated!
CodePudding user response:
The problem is that node.then()
doesn't return anything, so there is nothing to concat. Instead you would have to either pass a callback to .then()
which will handle concatenating the lists or use await
to wait till the value returned from get_properties()
is resolved and then concat the lists.
I would suggest something like this,
// Function to asynchronously concat the lists
const async_concat = async(oldList, value) => {
return oldList.concat(value);
}
// Make sure finalList is not const
var finalList = [];
while (countTimes < count) {
// await the value of node and asynchronously concat the lists
let node = await get_properties(records, countTimes);
finalList = await async_concat(finalList, node);
countTimes
};