【发布时间】:2020-09-21 04:16:58
【问题描述】:
我试过这个,但它不起作用: readFile 运行良好,它在与数据库的连接开始之前完成。 我的问题是尝试使用 Mongoose 将解析的 JSON obj (listingData) 用于 MongoDB。
let listingData;
fs.readFile('listings.json', 'utf8', (err, data) => {
try {
listingData = JSON.parse(data);
} catch (err) {
console.log('Error reading JSON file');
throw err;
}
//console.log(listingData); // PRINTS DATA CORRECTLY!
});
async function loadListings() {
try {
await Listing.insertMany(listingData); // imported.insertMany(JSON Parsed)
console.log("Listings inserted in the DB");
//process.exit();
} catch (error) {
console.log("Error importing listings to DB");
process.exit();
}
}
已解决,谢谢
【问题讨论】:
标签: node.js json mongodb mongoose