Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/12.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 如何等待更新查询更新集合? 在这里,我从测试集合中获取所有域,并通过发送HTTPS req和更新同一集合中的状态来检查它是否工作 问题是当我得到第一个域及其检查和更新状态时,我也得到了下一个域的状态,所以它抛出了一个mongo错误 因此,我想知道如何等到它在集合中的更新状态(当运行更新查询时,我需要停止检查域,直到它被更新)_Node.js_Mongodb - Fatal编程技术网

Node.js 如何等待更新查询更新集合? 在这里,我从测试集合中获取所有域,并通过发送HTTPS req和更新同一集合中的状态来检查它是否工作 问题是当我得到第一个域及其检查和更新状态时,我也得到了下一个域的状态,所以它抛出了一个mongo错误 因此,我想知道如何等到它在集合中的更新状态(当运行更新查询时,我需要停止检查域,直到它被更新)

Node.js 如何等待更新查询更新集合? 在这里,我从测试集合中获取所有域,并通过发送HTTPS req和更新同一集合中的状态来检查它是否工作 问题是当我得到第一个域及其检查和更新状态时,我也得到了下一个域的状态,所以它抛出了一个mongo错误 因此,我想知道如何等到它在集合中的更新状态(当运行更新查询时,我需要停止检查域,直到它被更新),node.js,mongodb,Node.js,Mongodb,我认为你必须使用异步函数,你用的是异步函数吗 例如: 我在哪里,怎么能在这里?我看到了 router.get("/get", function (req, respo) { Client.connect(URL, function (err, db) { if (err) throw err; var dbo = db.db("gvm"); dbo .collection("test")

我认为你必须使用异步函数,你用的是异步函数吗

例如:


我在哪里,怎么能在这里?我看到了
router.get("/get", function (req, respo) {
  Client.connect(URL, function (err, db) {
    if (err) throw err;
    var dbo = db.db("gvm");

    dbo
      .collection("test")
      .find({
        $and: [{ IsDeleted: false }, { TrackDomain: { $nin: [null, ""] } }],
      })
      .project({
        ClientID: "$_id",
        _id: 0,
        TrackDomain: 1,
        IsTrackDomainWorking: 1,
      })
      .toArray(function (error, sites) {
        if (err) throw error;
        
        sites.forEach((item) => {
          let TrackDomain = "https://" + item.TrackDomain;
          try {
            let isWorking = false;

            https.get(TrackDomain, async(res) => {
              if (res.statusCode == 200) {
                isWorking = true;
              }
              
              if (isWorking != item.IsTrackDomainWorking) {
                
                dbo.collection("test").findOneAndUpdate(
                  { $and: [{ _id: item.ClientID }, { IsDeleted: false }] },
                  {
                    $set: {
                      IsTrackDomainWorking: Boolean(isWorking),
                      LastUpdatedBy: item.ClientID,
                      LastUpdatedDate: new Date(),
                    },
                  },
                  function (err, data) {
                    if (err) throw err;
                    
                    db.close();
                  }
                );
              }
            });
          } catch (error) {
            console.log(error);
          }
        });
        respo.send("done");
      });
  });
});

// Here async function is created
auth.prototype.processSignIn = async (incomingUser, callback) => {

    var errors = {}

    // await is only can be used when function is async [ This waits for completion of process ]

    const loginQuery = await Accounts.findOne({
        email: incomingUser.email
    })
    
    if(loginQuery != null)
    {

        if(validatePassword(incomingUser.password, loginQuery.password) === true)
        {
            errors.message = 'success'
            errors.user = loginQuery
            // Hide Password
            loginQuery.password = null
            callback(errors)
        }
        else
        {
            errors.password = 'wrong_password'
            errors.message = 'failed'
            callback(errors)
        }
    }
    else
    {
        errors.email = 'wrong_creds'
        errors.message = 'failed'
        callback(errors)
    }

}