我正在使用neo4j js驱动程序(和async实用程序模块(编写nodejs应用程序,以使用项目源JS文件的数据在neo4j数据库中导入。
在我的测试项目文件夹" testapp"中,有2个文件夹,即"状态"one_answers" City"和一个" country.js" 文件。" country.js" 文件包含国家的名称和ID,并且该countryID用作文件夹"状态"one_answers" City"的JS文件中的参数。
- 来自country.js文件的摘要
const countries= [
{ id: "1", label: "USA" },
{ id: "2", label: "Scotland" }
];
module.exports = countries;
" state"文件夹的js文件具有这样的命名约定:( country-1-statelist.js , country-county-county-county-county-2-statelist.js 等等on(。
- 来自"状态"文件夹中的JS文件的示例。
const data = {"params":{"country_id":"1"},"items":
[{"id":"1","label":"Alaska"},{"id":"2","label":"Alabama"}]
module.exports = data;
类似地,"城市"文件夹的js文件具有这样的命名约定:( country-country-1-state-1-list.js , country-country-1-state-2--list.js 等(。
- 来自"城市"文件夹中的特定JS文件的示例
const data =
{"params":
{"country_id":"1","state_id":"1"},
"items":[
{"id":"1","label":"New York"},
{"id":"2","label":"Chicago"}
]
}
module.exports = data;
因此,我试图与标签国家,州和城市一起添加节点,以便在没有任何关系的情况下进口它们。但是我在尝试使用多链承诺将其导入我的测试dB时会遇到错误。
。因此,我能够从"状态"文件夹中导入country.js和其他JS文件中的数据。但是,在尝试从" City"文件夹的JS文件执行同样的事情时,我会得到"参考":未定义的"错误"错误。
我已经在nodejs中创建了index.js文件,其中具有以下代码:
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);
});
}
);
}
我是ES6,Cypher和Promise功能的新手,可能导致了此问题。虽然,我已经研究了与多个承诺链接相关的大多数帖子,博客和视频,但是当单个JS文件中存在多个参数以及如何使用这些参数来拉数据时,我仍然无法弄清楚如何执行此操作。使用Cypher查询。
现在任何帮助或建议对我来说真的很有帮助!预先感谢
在 insertState()
和 insertCity()
中,您正在尝试取消item.label
和item.id
,但是范围中没有item
。