设置node.js,因此遇到错误时不会崩溃
我目前正在为Web刮擦应用程序创建一个API,以将Web刮板数据馈送到MongoDB中。我在下面列出了我的代码,该代码将.csv文件转换为.json,然后将其插入MongoDB。问题是,我设置了一个索引,因此没有两个数据库条目是相同的。
每当发生这种情况时,它会引发错误(如它应该),但它会停止服务器的运行,这不好。我需要为此使用try/catch语句吗?我将如何格式化此尝试/捕获语句?我只需要它继续运行,即使数据库条目会引发错误。谢谢您的帮助!
csvtojson()
.fromFile("sample.csv")
.then(csvData => {
mongodb.connect(
url,
{ useNewUrlParser: true, useUnifiedTopology: true },
(err, client) => {
if (err) throw err;
client
.db("databaseName")
.collection("collectionName")
.insertMany(csvData, options);
}
);
});
I am currently creating an API for a web scraping application to feed data from the web scraper into MongoDB. I have listed my code below that converts a .csv file to .json and then inserts it into MongoDB. The issue is, I have an index set up so that no two database entries are identical.
Whenever this occurs, it throws an error (as it should), but it stops the running of the server which is not good. Would I need to use a try/catch statement for this? How would I format this for a try/catch statement? I just need it to continue running even if the database entry throws an error. Thank you for the assistance!
csvtojson()
.fromFile("sample.csv")
.then(csvData => {
mongodb.connect(
url,
{ useNewUrlParser: true, useUnifiedTopology: true },
(err, client) => {
if (err) throw err;
client
.db("databaseName")
.collection("collectionName")
.insertMany(csvData, options);
}
);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论