I am writing nodejs application using neo4j js driver (and async utility module) to use a project's source js files' data to be imported in neo4j database.
There are 2 folders namely "State"and "City" and a single "country.js" file in my test project folder "testapp". The "country.js" file contains the name and id of the countries and the countryid is used as a parameter in the js files present in the folders "State"and "City".
-- snippet from country.js file
const countries= [
{ id: "1", label: "USA" },
{ id: "2", label: "Scotland" }
];
module.exports = countries;
The "State" folder has js files which has naming convention like this: (Country-1-statelist.js, Country-2-statelist.js and so on).
--Sample from js file present in "State" folder.
const data = {"params":{"country_id":"1"},"items":
[{"id":"1","label":"Alaska"},{"id":"2","label":"Alabama"}]
module.exports = data;
Similarly, "City" folder has js files which has naming convention like this: (Country-1-state-1-list.js, Country-1-state-2-list.js and so on).
-- sample from a particular js file present in "City" folder
const data =
{"params":
{"country_id":"1","state_id":"1"},
"items":[
{"id":"1","label":"New York"},
{"id":"2","label":"Chicago"}
]
}
module.exports = data;
So,I am trying to add nodes with label Countries, States and Cities such that they would be imported without their relationship being unaltered. But I am getting error in nodejs while trying to use multiple chain promise to import it into my test db.
so, far I was able to import the data present in country.js and other js files from "State" folder. But while trying to do the same from js files of "City" folder, I am getting "ReferenceError: item is not defined" error.
I have created index.js file in nodejs to do so, which has the following code:
const neo4j = require('neo4j-driver').v1;
const driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "testapp"));
const session = driver.session();
const async = require('async');
const appPathway = '/Users/reactor/Documents/testapp';
const country = require(appPathway+'/country');
async.eachSeries(country, (item, callback) => {
const resPromise = session.run(
`CREATE (c:country {label: "${item.label}", id: "${item.id}"}) RETURN c.id`
);
resPromise
.then((data) => {
return insertState(item.id);
}
)
.then
(
(done) => {
callback(null);
}
)
.catch
(
(err) => {
callback(err);
}
);
},
(err) => {
console.log(err);
session.close();
driver.close();
}
);
function insertState(countryID) {
let state = require(appPathway+'/State/Country-'+countryID+'-statelist.js');
return new Promise((resolve, reject) =>{
async.eachSeries(state.items, (item1,item2, callback) => {
const resPromise = session.run(
`
CREATE (s:state {label: "${item.label}", id: "${item.id}"})
WITH s
MATCH (c:Country)
WHERE c.id = '${countryID}'
CREATE (s)-[r:LOCATED_IN]->(c)
RETURN c,s
`
);
resPromise
.then((state) => {
console.log(state);
callback(null);
})
resPromise
.then((data) => {
return insertCity(item1.id,item2.id);
})
.then((done) => {
callback(null);
}
)
.catch((err) => {
callback(err);
});
}, (err) => {
if(err) {
reject(err);
}
resolve(true);
});
}
);
}
// Problem rose when i inserted the following block of code !!
function insertCity(countryID,stateID) {
let city = require(appPathway+'/city/Country-'+countryID+'-state-'+stateID+'-list.js');
return new Promise((resolve, reject) =>
{
async.eachSeries(city.items, () => {
const resPromise = session.run(
`
CREATE (w:city {label: "${item.label}", id: "${item.id}"})
WITH w
MATCH (s:state)
WHERE s.id = '${stateID}'
/* How am I suppose to use the countryid parameter here in this cypher query ? As it doesn't have direct relation with label "country" in this query */
CREATE (w)-[r:PRESENT_IN]->(s)
RETURN w
`
);
resPromise
.then((city) => {
console.log(city);
callback(null);
})
.then((done) => {
callback(null);
})
.catch((err) => {
callback(err);
});
}, (err) => {
if(err) {
reject(err);
}
resolve(true);
});
}
);
}
I am new to ES6,cypher and promise function which may have caused this problem. Although, I have looked into most of the posts, blogs and videos related to multiple promise chaining, but still I am unable to figure out how to do it when multiple parameters are present in a single js file and how to use these parameters to pull the data using cypher query.
Any help or suggestion would be really helpful for me now ! Thanks in Advance
via Code Reactor
No comments:
Post a Comment