我一般都是Node和异步编程的新手,我无法解决这个问题。
我使用Mongoose将注册选民的数据导入MongoDB。可以想象,许多选民住在同一条街上。在导入过程中,我会检查街道是否已经存在。如果没有,我需要创建一个新的街道文档。如果是这样,我只想创建一个Address子文档。
我遇到的问题是我不知道如何处理find()Mongoose命令是异步的这一事实。所以在我的代码中发生的事情是,当解析CSV行时,所有的find命令都排队但不会立即执行。我的程序读了一行,看到了" Choctaw St"例如,并将find()命令排队以查看它是否存在。但是下一行也可能是Choctaw St,另一个find()命令将排队。在Mongo中创建文档发生在find()命令的回调中,该命令在执行所有find命令之后才可能运行。因此,当一个接一个地运行查找时,程序将尝试多次创建文档
以下是我的代码中的代码段:
Street.findOne(
{
campaign_id: user_id,
street_name: streetData.street_name,
city: streetData.city
},
function(err, foundStreet) {
if(err) {
console.log(err)
}
if(foundStreet) {
console.log('Found street: ' + foundStreet.street_name + '. Not adding!!!')
//Code for address insertion will go here.
} else {
console.log(streetData.street_name + ' not found. Adding now.')
let newStreet = new Street({
campaign_id: user_id,
street_name: streetData.street_name,
city: streetData.city
})
newStreet.save((err, streetDoc) => {
if(err) {
console.log('Error creating street: ' + newStreet.street_name)
}
if(streetDoc) {
console.log('Street created: ' + streetDoc.street_name)
}
})
}
})
以下是我的程序的输出,突出了我的问题。我提前道歉了。如您所见,尝试创建重复的街道文档时会抛出错误。
非常感谢任何帮助。我不能相信我的案子很特别,但我已经坚持了3天这个问题并且尚未找到解决方案。
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"DEERPATH RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"HUNTINGTON PL","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"SULLIVAN DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"QUAIL CREEK DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"N CHEROKEE AVE","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"STEEPLECHASE CT","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"LOCKWYNN TRCE","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"SHOREWOOD DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"TWIN OAKS LN","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"BAKER TRCE","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"N ENGLEWOOD AVE","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"PLANTATION RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"OVERLOOK DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"ADRIAN RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"DEERPATH RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"N CHEROKEE AVE","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"EMERALD LAKE DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"RADCLIFF RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"BAKER TRCE","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"GABRIEL CT","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"RENDALE RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"MILL RIDGE RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"CHOCTAW ST","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"RICHMOND RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"BRIARCLIFF RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"GIRARD AVE","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"HEATHERBROOK PL","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"CARDINAL CT","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"GARDENIA DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"HOUSTON ST","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"BUENA VISTA DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"MOCKINGBIRD LN","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"LASALLE DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"WHIFFLETREE WAY","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"LOBLOLLY LN","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"DEERPATH RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"FOXRIDGE RD","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"AZURE DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"SHADYBROOK LN","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"FAIRFIELD DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"AZURE DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"QUAIL CREEK DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"SUMMERRAIN TER","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"FAIRFIELD DR","city":"DOTHAN","addresses":[]}
DEERPATH RD not found. Adding now.
STEEPLECHASE CT not found. Adding now.
QUAIL CREEK DR not found. Adding now.
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"HUNTINGTON PL","city":"DOTHAN","addresses":[]}
N CHEROKEE AVE not found. Adding now.
Street created: DEERPATH RD
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"N PONTIAC AVE","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"N CHEROKEE AVE","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"IMPALA DR","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"BAKER TRCE","city":"DOTHAN","addresses":[]}
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"AZURE DR","city":"DOTHAN","addresses":[]}
Street created: STEEPLECHASE CT
BAKER TRCE not found. Adding now.
DEERPATH RD not found. Adding now.
GABRIEL CT not found. Adding now.
BRIARCLIFF RD not found. Adding now.
HOUSTON ST not found. Adding now.
LOBLOLLY LN not found. Adding now.
FAIRFIELD DR not found. Adding now.
TWIN OAKS LN not found. Adding now.
ADRIAN RD not found. Adding now.
BAKER TRCE not found. Adding now.
RICHMOND RD not found. Adding now.
GARDENIA DR not found. Adding now.
WHIFFLETREE WAY not found. Adding now.
SHADYBROOK LN not found. Adding now.
FAIRFIELD DR not found. Adding now.
SULLIVAN DR not found. Adding now.
SHOREWOOD DR not found. Adding now.
OVERLOOK DR not found. Adding now.
RADCLIFF RD not found. Adding now.
N ENGLEWOOD AVE not found. Adding now.
N CHEROKEE AVE not found. Adding now.
RENDALE RD not found. Adding now.
GIRARD AVE not found. Adding now.
BUENA VISTA DR not found. Adding now.
DEERPATH RD not found. Adding now.
AZURE DR not found. Adding now.
Street created: N CHEROKEE AVE
Adding streetData: {"campaign_id":"59dcde03ea6bdc44cb51b655","street_name":"HAYES DR","city":"DOTHAN","addresses":[]}
Street created: QUAIL CREEK DR
N PONTIAC AVE not found. Adding now.
AZURE DR not found. Adding now.
CHOCTAW ST not found. Adding now.
CARDINAL CT not found. Adding now.
LASALLE DR not found. Adding now.
AZURE DR not found. Adding now.
SUMMERRAIN TER not found. Adding now.
HUNTINGTON PL not found. Adding now.
BAKER TRCE not found. Adding now.
Found street: N CHEROKEE AVE. Not adding!!!
HUNTINGTON PL not found. Adding now.
LOCKWYNN TRCE not found. Adding now.
PLANTATION RD not found. Adding now.
EMERALD LAKE DR not found. Adding now.
MILL RIDGE RD not found. Adding now.
HEATHERBROOK PL not found. Adding now.
MOCKINGBIRD LN not found. Adding now.
FOXRIDGE RD not found. Adding now.
QUAIL CREEK DR not found. Adding now.
IMPALA DR not found. Adding now.
Street created: HOUSTON ST
Street created: BAKER TRCE
Street created: FAIRFIELD DR
Street created: N ENGLEWOOD AVE
Error creating street: DEERPATH RD
Street created: GARDENIA DR
Street created: SHOREWOOD DR
Street created: RENDALE RD
Error creating street: DEERPATH RD
Error creating street: FAIRFIELD DR
Street created: BRIARCLIFF RD
Street created: ADRIAN RD
Street created: SHADYBROOK LN
Street created: RADCLIFF RD
Street created: BUENA VISTA DR
Street created: GABRIEL CT
Street created: TWIN OAKS LN
Street created: WHIFFLETREE WAY
Street created: OVERLOOK DR
Street created: GIRARD AVE
Street created: LOBLOLLY LN
Street created: RICHMOND RD
Street created: SULLIVAN DR
Street created: AZURE DR
Error creating street: BAKER TRCE
Error creating street: N CHEROKEE AVE
HAYES DR not found. Adding now.
Street created: CHOCTAW ST
Error creating street: AZURE DR
Street created: N PONTIAC AVE
Street created: HUNTINGTON PL
Error creating street: AZURE DR
Street created: CARDINAL CT
Street created: MILL RIDGE RD
Error creating street: BAKER TRCE
Street created: LASALLE DR
Street created: EMERALD LAKE DR
Error creating street: QUAIL CREEK DR
Street created: IMPALA DR
Street created: SUMMERRAIN TER
Street created: PLANTATION RD
Street created: LOCKWYNN TRCE
Street created: MOCKINGBIRD LN
Street created: FOXRIDGE RD
Street created: HEATHERBROOK PL
Error creating street: HUNTINGTON PL
Street created: HAYES DR