Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/35.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
Node.js 承诺中有catch-Nodejs的未处理catch错误_Node.js_Express_Redis_Azure Redis Cache - Fatal编程技术网

Node.js 承诺中有catch-Nodejs的未处理catch错误

Node.js 承诺中有catch-Nodejs的未处理catch错误,node.js,express,redis,azure-redis-cache,Node.js,Express,Redis,Azure Redis Cache,我有一个从mongodb或azure redis缓存获取数据的函数。我试图将其设置为首先检查缓存,然后在缓存为空时点击mongodb,但出于某种原因,它首先执行mongodb代码。我创建了一个处理订单的承诺,但它给了我一个未处理的承诺拒绝错误。我有一个陷阱,所以我不明白问题是什么。谢谢你的帮助 控制台输出 SETTING TRUE... (node:27068) UnhandledPromiseRejectionWarning: Cache found (node:27068) Unhandl

我有一个从mongodb或azure redis缓存获取数据的函数。我试图将其设置为首先检查缓存,然后在缓存为空时点击mongodb,但出于某种原因,它首先执行mongodb代码。我创建了一个处理订单的承诺,但它给了我一个未处理的承诺拒绝错误。我有一个陷阱,所以我不明白问题是什么。谢谢你的帮助

控制台输出


SETTING TRUE...
(node:27068) UnhandledPromiseRejectionWarning: Cache found
(node:27068) 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)
(node:27068) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero
exit code.

获取请求



  const promise = new Promise((resolve, reject) => {
      client.get("directory", (err, reply) => {
        if (reply !== null) {
          console.log("SETTING TRUE...");
          redisReply = true;

          replyConverted = JSON.parse(reply);

          res.status(200).json({
            message: "Directory retrieved successfully!",
            posts: replyConverted,
            maxPosts: replyConverted
          });
          reject("Cache found);
        } else {
          resolve();
        }
      });
    }).then(res => {
      const postPerPage = 20;
      console.log("directory ID SECTION");

      var searchKey = new RegExp(req.query.keyword, "i");

      console.log("req.query.currentPage");
      console.log(req.query.currentPage);
      console.log(req.query.keyword);
      let currentPage = req.query.currentPage;

      console.log("REDIS RELY IS " + redisReply);
      console.log("GRABBING FROM COSMOSDB");
      User.countDocuments({
        $and: [{ username: searchKey }, { role: "Seller" }]
      })
        .then(docs => {
          console.log("COUNT");
          console.log(docs);
          let totalPosts = docs;
          User.find({
            $and: [{ username: searchKey }, { role: "Seller" }]
          })
            .select("_id username")
            .skip(postPerPage * (currentPage - 1))
            .limit(postPerPage)
            .then(documents => {
              res.status(200).json({
                message: "Directory retrieved successfully!",
                posts: documents,
                maxPosts: totalPosts
              });

              let docsString = JSON.stringify(docs);
              client.set("directory", docsString, (err, reply) => {
                console.log(reply);
              });
            });
        })
        .catch(err => {
          console.log("Skipped Mongodb");
        });


您可以使用一个函数来首先搜索缓存(如果找到),如果不进行查询则使用它,然后使用结果进行回调。在您的情况下,它可能看起来像这样,您也可能希望使键动态地与您的查询相对应

module.exports.findDirectory = function(client,query, callback){
    client.get(query, (err, reply) => {
     if (err) {
       callback(null);
    } else if(reply){

    callback(JSON.parse(reply));

    } else {
    User.countDocuments({
            $and: [{ username: query.searchKey }, { role: "Seller" }]
          })
            .then(docs => {
              console.log("COUNT");
              console.log(docs);
              let totalPosts = docs;
              let postsPerPage = 9;
              User.find({
                $and: [{ username: query.searchKey }, { role: "Seller" }]
              })
                .select("_id username")
                .skip(postPerPage * (query.currentPage - 1))
                .limit(postPerPage)
                .then(documents => {


         let docsString = JSON.stringify(docs);
         client.set(query, docsString, (err, reply) => {
                    console.log(reply);
                    callback(docs)
           });
       });
    }

  })

}
然后在这样的路线上使用它

dirsController.findDirectory(client,req.query,function(dirs){

   res.send(dirs)
}

我认为你把拒绝和决心混淆在一起了。此外,当您在redis中找到缓存查询时,您应该返回,以便停止执行。当找到缓存时,我将拒绝它,以便它不会命中数据库。若我真的解决了,那个么它将转到下一个。当找到缓存时,我不希望看到下一个。您是否有可能提供一个如何正确执行的示例?它是说查询未定义,因为我使用的是client.set with directory,然后它是说回调未定义。我对您的代码进行了一些修改,以适应我的用例,现在它已成功返回数据。如果你有任何反馈的话。让我知道!