2017-07-28 71 views
0

所以我的目標 - >編寫一個腳本來從csv中提取數據並將其轉儲到我的數據庫中。所以一切似乎都在起作用,除了我無法在不擦除任何數據(或不全部寫入數據庫)的情況下正確退出進程。Node.js,Mongodb,Monk,PapaParse,Bluebird,DB不會更新

db.then((db) => { 
    console.log('connected to db'); 
    const creators = db.get('creators'); 

    let files = fs.readdirAsync(folder) 
    .catch(err => { 
     exitOnErr(err, 'error reading directory'); 
    }) 
    .map(filename => { 
     return processFile(filename) 
     .then(content => { 
      return Papa.parsePromise(content); 
     }) 
     .then(results => { 
      db_add(results, creators); 
     }); 
    }) 
    .then(() => { 
     console.log('Completed parsing files..'); 
     /* close db and exit the process -> if I do this here, 
      then everything exits without doing anything, but if 
      I let the process keep running, then the database 
      gets properly updated */ 
    }); 
}).catch((err) => { 
    exitOnErr(err, 'error connecting to db'); 
}); 

哪裏是合適的地方關閉數據庫並退出進程?無法弄清楚這裏的承諾方案在哪裏適合它。

回答

0

想通了。更新後的代碼:

db.then((db) => { 
    console.log('connected to db'); 
    const creators = db.get('creators'); 

    return fs.readdirAsync(folder) 
    .catch(err => { 
     exitOnErr(err, 'error reading directory'); 
    }) 
    .map(filename => { 
     return processFile(filename) 
     .then(content => { 
      return Papa.parsePromise(content); 
     }) 
     .then(results => { 
      return db_add(results, creators); 
     }); 
    }); 
}).catch((err) => { 
    exitOnErr(err, 'error connecting to db'); 
}).then(() => { 
    console.log('Completed parsing files...'); 
    closeAndExit(0); 
}); 

沒有正確地回覆鏈上的承諾。