设置node.js,因此遇到错误时不会崩溃

发布于 2025-02-03 06:03:36 字数 666 浏览 0 评论 0原文

我目前正在为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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文