没有循环节点JS将数据插入变量

发布于 2025-01-27 16:48:37 字数 785 浏览 2 评论 0原文

我尝试将数据插入变量中,并在节点js内部函数中循环循环,但是当我尝试将数据推入电报bot时,变量为空,如何正确地将数据插入变量。

错误:

代码:

async function brokeServer(listServer) {
    let nothing = '';
    for (const newServer of listServer) {
        
        ping.sys.probe(newServer.srv_ip, async function(isAlive){
            let msg = isAlive ? 'host ' + newServer.srv_ip + ' is alive' : 'host ' + newServer.srv_ip + ' is dead';

            //console.log(msg);

            let myspace = '\n';
            nothing+=myspace;
            nothing+=msg;

              
        });
    
    }

    MeikelBot.sendMessage(-721865824, nothing);
    
}

i try to insert data into variable with looping on node js inside function, but when i try to push the data into telegram bot, the variable is empty, how to insert the data into the variable properly.

the error :
enter image description here

the code :

async function brokeServer(listServer) {
    let nothing = '';
    for (const newServer of listServer) {
        
        ping.sys.probe(newServer.srv_ip, async function(isAlive){
            let msg = isAlive ? 'host ' + newServer.srv_ip + ' is alive' : 'host ' + newServer.srv_ip + ' is dead';

            //console.log(msg);

            let myspace = '\n';
            nothing+=myspace;
            nothing+=msg;

              
        });
    
    }

    MeikelBot.sendMessage(-721865824, nothing);
    
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜血缘 2025-02-03 16:48:37

它的原因是ping.sys.probe仍然具有异步且尚未正确处理。您需要将其转换为Promise,然后应该与async函数一起使用&amp; <代码> 循环。

示例(请注意,未经测试):

function ProbePromise(server_ip) {
    return new Promise((resolve) => {
        ping.sys.probe(server_ip, async function(isAlive){
            resolve(isAlive)
        })
    })
}

async function brokeServer(listServer) {
    let nothing = '';
    for (const newServer of listServer) {
        // use the promise version of Probe
        let isAlive = await ProbePromise(newServer.srv_ip)
        let msg = isAlive ? 'host ' + newServer.srv_ip + ' is alive' : 'host ' + newServer.srv_ip + ' is dead';
        //console.log(msg);
        let myspace = '\n';
        nothing+=myspace;
        nothing+=msg;
    }

    MeikelBot.sendMessage(-721865824, nothing);
}

Its because the ping.sys.probe is still asynchronous and not yet handled properly. You need to turn it to Promise then it should work with the async function & for loop.

Example (please note this is untested):

function ProbePromise(server_ip) {
    return new Promise((resolve) => {
        ping.sys.probe(server_ip, async function(isAlive){
            resolve(isAlive)
        })
    })
}

async function brokeServer(listServer) {
    let nothing = '';
    for (const newServer of listServer) {
        // use the promise version of Probe
        let isAlive = await ProbePromise(newServer.srv_ip)
        let msg = isAlive ? 'host ' + newServer.srv_ip + ' is alive' : 'host ' + newServer.srv_ip + ' is dead';
        //console.log(msg);
        let myspace = '\n';
        nothing+=myspace;
        nothing+=msg;
    }

    MeikelBot.sendMessage(-721865824, nothing);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文