Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 我的NodeJs给出了未处理承诺的错误_Javascript_Node.js_Postman_Atom Editor_Nodemon - Fatal编程技术网

Javascript 我的NodeJs给出了未处理承诺的错误

Javascript 我的NodeJs给出了未处理承诺的错误,javascript,node.js,postman,atom-editor,nodemon,Javascript,Node.js,Postman,Atom Editor,Nodemon,在我在这里添加异步函数之前,一切都在运行。原因可能是什么?在Tour.create中发生了什么 可能是您在新承诺((解析,拒绝){…})中调用Promise.reject(),而不是使用reject() 下面是我的“bla”函数中拒绝和解决案例的代码: exports.createTour = async (req, res) => { try { const newTour = await bla(); res.status(201).json


在我在这里添加异步函数之前,一切都在运行。原因可能是什么?

在Tour.create中发生了什么

可能是您在
新承诺((解析,拒绝){…})中调用
Promise.reject()
,而不是使用
reject()

下面是我的“bla”函数中拒绝和解决案例的代码:

exports.createTour = async (req, res) => {
      try {
        const newTour = await bla();
        res.status(201).json({
          status: "success",
          data: {
            tours: newTour,
          },
        });
      } catch (err) {
        res.status(400).json({
          status: "fail",
          message: err,
        });
      }
});

    
    let bla=()=>{
      return new Promise((resolve, reject)=>{
        //resolve("yay");
        reject("I have my reasons");
      });
    }
我会尽力解释我怀疑发生了什么事- 但如果没有看到代码,就很难知道:

let create = ()=>{
      // option 1. returns a new promise that resolves - your catch should work 
      Promise.resolve("great success");

      return new Promise((resolve, reject)=>{
        // option 2. creates a new promise in the new promise, which would explain the situation you described
        Promise.reject("I need to be handled too - but im not");

        // option 3. handles the locally created and rejected promise
        Promise.reject("I need to be handled too").catch(x => console.log("handled"));

        // option 4. rejects and is caught in the createTour function that called us - your catch should work  
        reject("I have my reasons");

       // option 5. another function you call rejects and is not handled - would also cause what you described, for the same reason
       let result = await func();
      });
    }
试着看看你是否有某种选择5或2——它在承诺中创造了一个承诺,拒绝(并且没有得到处理)

另外,请注意,您可以使用

await Tour.create().catch(err => ...);
(如选项2)并专门处理拒绝

如果您想阅读更多:

这个
.json
方法是同步的吗?在
catch
语句中是否引发异常?bla函数是用户定义的,而create是预定义的,这有什么区别吗?Tour.create在何处以及如何定义?我以为这是你的密码。。如果它只是在另一个文件中,它应该不会有什么区别。我编辑了它,以显示拒绝的不同方式,以及根据您发布的内容,我认为正在发生的事情。。它可能是你调用的另一个方法的内部create,它返回拒绝而不处理它..createTour之前有一个函数,这导致了问题,所以我不得不删除它。
await Tour.create().catch(err => ...);