简体   繁体   English

为什么会收到“ UnhandledPromiseRejectionWarning”,这是什么意思?

[英]Why do I get “ UnhandledPromiseRejectionWarning” and what does it mean?

I am trying to read and write to a CSV file, and I am quite new to javascript. 我正在尝试读取和写入CSV文件,而我对javascript还是很陌生。 My code seems to make sense to me but I am not sure why it isn't running. 我的代码对我来说似乎很有意义,但是我不确定为什么它没有运行。

app.post('/name=:name/available=:available/type=:type/subtype=:subtype/ip=:ip', (req, res) => {
  csvtojson().fromFile(csvFilePath).then((objects) => {
    objectsArray = objects;
    const newItem = {
      id: objectsArray.length + 1, 
      name: req.params.name,
      available: req.params.available
    };

    csvWriter.writeRecords(objectsArray).then( () => {
      console.log("Csv File Created!");
      console.log(objectsArray);
      res.json(objectsArray);
    });
   });
});

From what I can see, I should first read the CSV file, then save the contents in an array, then create an object, then write the object into the CSV file. 从我所看到的,我应该首先读取CSV文件,然后将内容保存在数组中,然后创建一个对象,然后将该对象写入CSV文件。 But I get the errors: "UnhandledPromiseRejectionWarning: Error: EBUSY: resource busy or locked", and "UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)" 但是我得到了以下错误:“ UnhandledPromiseRejectionWarning:错误:EBUSY:资源繁忙或锁定”,以及“ UnhandledPromiseRejectionWarning:未处理的Promise拒绝。此错误是由于在没有catch块的情况下抛出了异步函数而引起的,或者是因为未使用.catch()处理。(拒绝ID:1)”

The UnhandledPromiseRejectionWarning comes becomes one of your two asynchronous operations is encountering an EBUSY error and you have no error handler for that error. UnhandledPromiseRejectionWarning成为您的两个异步操作之一,遇到EBUSY错误,并且您没有该错误的错误处理程序。 The node.js runtime sees that a promise was rejected, but there was no error handler for that and identifies that as a likely inappropriate coding situation (a bug) so it emits the warning. node.js运行时看到承诺被拒绝,但是没有错误处理程序,并将其识别为可能的不适当编码情况(错误),因此发出警告。

In Javascript, EVERY promise operation that can ever reject must have an error handler. 在Javascript中,每个可以拒绝的promise操作都必须具有错误处理程序。 An error handler for promise rejections can be done with a .catch() , a try/catch if the promise is await ed or with the 2nd argument to .then() . 对于承诺拒绝的错误处理程序可以用做.catch()一个try/catch ,如果许是await版或与第二个参数.then()

You can add proper error handlers to your code and that will tell you a bit more about where the error is occurring and will get rid of the UnhandledPromiseRejectionWarning . 您可以在代码中添加适当的错误处理程序,这将使您更加了解错误发生的位置,并摆脱UnhandledPromiseRejectionWarning

app.post('/name=:name/available=:available/type=:type/subtype=:subtype/ip=:ip', (req, res) => {
  csvtojson().fromFile(csvFilePath).then((objects) => {
    const newItem = {
      id: objects.length + 1, 
      name: req.params.name,
      available: req.params.available
    };

    return csvWriter.writeRecords(objects).then( () => {
      console.log("Csv File Created!");
      console.log(objects);
      res.json(objects);
    });
   }).catch(err => {
      console.log(err);
      res.sendStatus(500);
   });
});

If, for debugging purposes, you wanted to log the 2nd promise rejection separately, you could do this: 如果出于调试目的,您想要单独记录第二个承诺拒绝,则可以执行以下操作:

app.post('/name=:name/available=:available/type=:type/subtype=:subtype/ip=:ip', (req, res) => {
  csvtojson().fromFile(csvFilePath).then((objects) => {
    const newItem = {
      id: objects.length + 1, 
      name: req.params.name,
      available: req.params.available
    };

    return csvWriter.writeRecords(objects).then( () => {
      console.log("Csv File Created!");
      console.log(objects);
      res.json(objects);
    }).catch(err => {
      console.log("csvWriter error", err);
      throw err;
    });
   }).catch(err => {
      console.log(err);
      res.sendStatus(500);
   });
});

In this case, I've returned the 2nd promise which will chain it to the 1st promise so we can then add a single .catch() handler to the 1st promise and it will catch errors from either promise. 在这种情况下,我返回了第二个.catch() ,它将第二个.catch()到第一个.catch()因此我们可以向第一个.catch()添加单个.catch()处理程序,它将捕获来自任何一个.catch()错误。 Then, the error handler sends a 500 status to complete the request handler. 然后,错误处理程序发送500状态以完成请求处理程序。

I would guess that the EBUSY error has something to do with either reading from the file represented at csvFilePath or with the file that csvWriter is trying to write to. 我想EBUSY错误与从csvFilePath表示的文件中csvFilePath或与csvWriter尝试写入的文件有关。 It sounds like it could be a file exclusivity issue where it's opened for exclusive access while doesn't allow other readers/writers to have access to it. 听起来这可能是文件排他性的问题,在打开该文件时可以进行独占访问,而不允许其他读取器/写入器访问。

We can't really help with that specific error without seeing more of the context of your code and understanding while files might have multiple readers/writers that creates some sort of contention. 如果看不到代码的更多上下文并理解文件可能具有多个读写器而导致某种争执的情况,我们就无法真正解决该特定错误。


FYI, it also looks like you should get rid of the objectsArray variable and just use objects instead. 仅供参考,看起来您应该摆脱objectsArray变量,而只使用objects objectsArray is not declared locally within this function. 在此函数内未在本地声明objectsArray If it is not declared at all, then it's an implicit global (which is bad). 如果根本没有声明,那么它是一个隐式全局(这很糟糕)。 If it is declared at a higher scope, then it's a variable that is potentially shared between users each making the same request (very bad bug). 如果在更高的范围内声明它,那么它是一个可能在每个都发出相同请求的用户之间共享的变量(非常严重的错误)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会收到 404 错误,没有 Firebase 应用“[DEFAULT]”是什么意思? - Why do I get a 404 error and what does no Firebase App '[DEFAULT]' mean? 为什么会收到UnhandledPromiseRejectionWarning:未处理的承诺被拒绝? - Why I get UnhandledPromiseRejectionWarning: Unhandled promise rejection? 如果我用jquery达到“最高”,那是什么意思? - If I get “top” with jquery, what does it mean? 为什么即使我有catch()函数,为什么也会收到UnhandledPromiseRejectionWarning? - Why do I get an UnhandledPromiseRejectionWarning even though I have a catch() function? 当我在 jQuery 中运行 $.get 时,我收到“object Object”。 为什么? 究竟是什么意思? - When I run $.get in jQuery I receive "object Object". Why? What does mean exactly? 这个错误“null is not an object”是什么意思? 我该怎么办? - What does this error "null is not an object "mean? And what should I do? 为什么我在尝试使用 chromedriver 打开浏览器时收到 UnhandledPromiseRejectionWarning - Why I get UnhandledPromiseRejectionWarning when trying to open a browser using chromedriver 为什么我收到“(node:7424)UnhandledPromiseRejectionWarning”消息以获取已处理的错误? - Why I get the “(node:7424) UnhandledPromiseRejectionWarning” message for handled error? “ &gt;&gt;&gt;&gt;”或“ &lt;&lt;&lt;&lt;”在JavaScript中的含义或作用 - What does “>>>>” or “<<<<” mean or do in Javascript if语句中的in表示什么或意味着什么? - What does in do or mean in an if statement?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM