Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/43.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 同步节点js函数_Javascript_Node.js_Asynchronous_Server - Fatal编程技术网

Javascript 同步节点js函数

Javascript 同步节点js函数,javascript,node.js,asynchronous,server,Javascript,Node.js,Asynchronous,Server,我对java脚本和节点js非常陌生。 我调用的一个简单函数有一个问题,它不止一次被调用。 这是我的密码 app.post('/checkGetSensorIds', function (req, res) { var tables=['temperature', 'pressure', 'linear_acceleration']; var ids= [1]; DButils.checkAllSensorsForId(connection, 1 , tables , function

我对java脚本和节点js非常陌生。 我调用的一个简单函数有一个问题,它不止一次被调用。 这是我的密码

app.post('/checkGetSensorIds', function (req, res) {
  var tables=['temperature', 'pressure', 'linear_acceleration'];
  var ids= [1];
  DButils.checkAllSensorsForId(connection, 1 , tables , function(idHasSensorsInfo){
    console.log("idHasSensorsInfo is: \n" , idHasSensorsInfo);
  });
  res.end();
});


/*this function gets a user Id, and the table of all sensors the customer wants, and return true if this 
user id has information in all the sesnsor tables that were requested, otherwise returns false*/
exports.checkAllSensorsForId=  function(dbConnection, id , sensorsTables, callback){
    var sensorsTablesLength= sensorsTables.length;
    for (var i = 0; i < sensorsTables.length; i++) {
        var tableName= sensorsTables[i];
        DButils.checkSingleSensorForId(dbConnection, id, tableName, function(idHasSensorInfo){
            if(idHasSensorInfo == false){
                callback(false);
                return;
            }
            //in case user have all info in db, we get here and need to return false
            if(i == sensorsTablesLength){
                callback(true);
                return;
            }
        });
    }
};


/*this function gets a user Id, and a single sensor table, and returns true if the user has information
in the requested sensor table, otherwise returns false*/
exports.checkSingleSensorForId=  function(dbConnection , id , sensorTable, callback){
    var myQuery = 'SELECT count(*) as IdCount FROM ' + sensorTable + ' WHERE id= ' + id;
    var query = dbConnection.query(myQuery, function (err, row, result) {       
        console.log(query.sql);
        if (err) {
            console.log("checkSingleSensorForId error");
            console.error(err);
            return;
        }
        var count= row[0].IdCount;
        var idHasSensorInfo = (count > 0);
        callback(idHasSensorInfo);
    });
};
console.LogidAssessorsInfo是:\n,idHasSensorsInfo;是一个调用了3次的行,而应该只调用一次

有人知道为什么,以及我需要做什么来修复它吗?

你有这句话:

DButils.checkAllSensorsForId(connection, 1 , tables , function(idHasSensorsInfo){
    console.log("idHasSensorsInfo is: \n" , idHasSensorsInfo);
});
那么你有这个:

exports.checkAllSensorsForId=  function(dbConnection, id , sensorsTables, callback){
    ...
    for (var i = 0; i < sensorsTables.length; i++) {
        ...
        callback();
        ...
    }
};
因此,回调行的调用次数与您调用它的次数相同,在您的例子中可能是3次-它所做的只是从上面调用函数,因此您会看到它被调用了3次

我不确定您到底想做什么,但是如果回调应该只调用一次,请确保它只运行一次-如果它应该“取消”for-在for中添加一个条件,或者在您准备好时使用承诺来解决