Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.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 nodej被困在回调中_Node.js - Fatal编程技术网

Node.js nodej被困在回调中

Node.js nodej被困在回调中,node.js,Node.js,我有麻烦,我的节点应用程序突然开始消耗大量的CPU。我怀疑下面的函数不知怎么卡住了 Client.prototype.countActiveChatsRedis = function (userID, agentID, obj, callback) { var count = 0; pub.keys("widgetActive:" + userID + ":*", function(err, key) { if(err !== null) {

我有麻烦,我的节点应用程序突然开始消耗大量的CPU。我怀疑下面的函数不知怎么卡住了

Client.prototype.countActiveChatsRedis = function (userID, agentID, obj, callback) {
    var count = 0;

    pub.keys("widgetActive:" + userID + ":*", function(err, key) {

        if(err !== null) {
            console.log("Redis error..... --> " + err);
            callback(count, obj);           
        }

        if(key && key.length > 0) {
            pub.mget(key, function(err, data) {
                if(data) {
                    for(var i = 0; i < data.length;i++) {
                        if(data[i]) {
                            var arr = data[i].split(",");

                            if(arr[2] == agentID) {
                                if (Number(arr[3]) > 0) {
                                    count++;
                                }                           
                            }
                        }
                    }

                    callback(count, obj);
                }
            });         
        } else {
            callback(count, obj);           
        }
    });
}
Client.prototype.countActiveChatsRedis=function(userID、agentID、obj、callback){
var计数=0;
pub.keys(“widgetActive:+userID+”:*”,函数(err,key){
如果(错误!==null){
log(“Redis错误…”-->“+err);
回调(计数,obj);
}
如果(key&&key.length>0){
pub.mget(键、函数(错误、数据){
如果(数据){
对于(变量i=0;i0){
计数++;
}                           
}
}
}
回调(计数,obj);
}
});         
}否则{
回调(计数,obj);
}
});
}
你知道问题出在哪里吗?在任何情况下都可以避免发送回调


此函数大约每秒运行50次。

您应该始终返回您的回调,以确保函数正确终止并将控件返回到调用上下文:

Client.prototype.countActiveChatsRedis = function (userID, agentID, obj, callback) {
var count = 0;

pub.keys("widgetActive:" + userID + ":*", function(err, key) {

    if(err !== null) {
        console.log("Redis error..... --> " + err);
        return callback(count, obj);           
    }

    if(key && key.length > 0) {
        pub.mget(key, function(err, data) {
            if(data) {
                for(var i = 0; i < data.length;i++) {
                    if(data[i]) {
                        var arr = data[i].split(",");

                        if(arr[2] == agentID) {
                            if (Number(arr[3]) > 0) {
                                count++;
                            }                           
                        }
                    }
                }

                return callback(count, obj);
            }
        });         
    } else {
        return callback(count, obj);           
    }
});
}
Client.prototype.countActiveChatsRedis=function(userID、agentID、obj、callback){
var计数=0;
pub.keys(“widgetActive:+userID+”:*”,函数(err,key){
如果(错误!==null){
log(“Redis错误…”-->“+err);
返回回调(计数,obj);
}
如果(key&&key.length>0){
pub.mget(键、函数(错误、数据){
如果(数据){
对于(变量i=0;i0){
计数++;
}                           
}
}
}
返回回调(计数,obj);
}
});         
}否则{
返回回调(计数,obj);
}
});
}

您应该始终返回您的回调,以确保函数正确终止并将控件返回到调用上下文:

Client.prototype.countActiveChatsRedis = function (userID, agentID, obj, callback) {
var count = 0;

pub.keys("widgetActive:" + userID + ":*", function(err, key) {

    if(err !== null) {
        console.log("Redis error..... --> " + err);
        return callback(count, obj);           
    }

    if(key && key.length > 0) {
        pub.mget(key, function(err, data) {
            if(data) {
                for(var i = 0; i < data.length;i++) {
                    if(data[i]) {
                        var arr = data[i].split(",");

                        if(arr[2] == agentID) {
                            if (Number(arr[3]) > 0) {
                                count++;
                            }                           
                        }
                    }
                }

                return callback(count, obj);
            }
        });         
    } else {
        return callback(count, obj);           
    }
});
}
Client.prototype.countActiveChatsRedis=function(userID、agentID、obj、callback){
var计数=0;
pub.keys(“widgetActive:+userID+”:*”,函数(err,key){
如果(错误!==null){
log(“Redis错误…”-->“+err);
返回回调(计数,obj);
}
如果(key&&key.length>0){
pub.mget(键、函数(错误、数据){
如果(数据){
对于(变量i=0;i0){
计数++;
}                           
}
}
}
返回回调(计数,obj);
}
});         
}否则{
返回回调(计数,obj);
}
});
}

您应该始终返回您的回调,以确保函数正确终止并将控件返回到调用上下文:

Client.prototype.countActiveChatsRedis = function (userID, agentID, obj, callback) {
var count = 0;

pub.keys("widgetActive:" + userID + ":*", function(err, key) {

    if(err !== null) {
        console.log("Redis error..... --> " + err);
        return callback(count, obj);           
    }

    if(key && key.length > 0) {
        pub.mget(key, function(err, data) {
            if(data) {
                for(var i = 0; i < data.length;i++) {
                    if(data[i]) {
                        var arr = data[i].split(",");

                        if(arr[2] == agentID) {
                            if (Number(arr[3]) > 0) {
                                count++;
                            }                           
                        }
                    }
                }

                return callback(count, obj);
            }
        });         
    } else {
        return callback(count, obj);           
    }
});
}
Client.prototype.countActiveChatsRedis=function(userID、agentID、obj、callback){
var计数=0;
pub.keys(“widgetActive:+userID+”:*”,函数(err,key){
如果(错误!==null){
log(“Redis错误…”-->“+err);
返回回调(计数,obj);
}
如果(key&&key.length>0){
pub.mget(键、函数(错误、数据){
如果(数据){
对于(变量i=0;i0){
计数++;
}                           
}
}
}
返回回调(计数,obj);
}
});         
}否则{
返回回调(计数,obj);
}
});
}

您应该始终返回您的回调,以确保函数正确终止并将控件返回到调用上下文:

Client.prototype.countActiveChatsRedis = function (userID, agentID, obj, callback) {
var count = 0;

pub.keys("widgetActive:" + userID + ":*", function(err, key) {

    if(err !== null) {
        console.log("Redis error..... --> " + err);
        return callback(count, obj);           
    }

    if(key && key.length > 0) {
        pub.mget(key, function(err, data) {
            if(data) {
                for(var i = 0; i < data.length;i++) {
                    if(data[i]) {
                        var arr = data[i].split(",");

                        if(arr[2] == agentID) {
                            if (Number(arr[3]) > 0) {
                                count++;
                            }                           
                        }
                    }
                }

                return callback(count, obj);
            }
        });         
    } else {
        return callback(count, obj);           
    }
});
}
Client.prototype.countActiveChatsRedis=function(userID、agentID、obj、callback){
var计数=0;
pub.keys(“widgetActive:+userID+”:*”,函数(err,key){
如果(错误!==null){
log(“Redis错误…”-->“+err);
返回回调(计数,obj);
}
如果(key&&key.length>0){
pub.mget(键、函数(错误、数据){
如果(数据){
对于(变量i=0;i0){
计数++;
}                           
}
}
}
返回回调(计数,obj);
}
});         
}否则{
返回回调(计数,obj);
}
});
}

在生产环境中使用密钥是一种不好的做法。引用Redis大师自己的话: