简体   繁体   English

在do-while循环中带有await的异步函数

[英]async function with await fetch inside do-while loop

I have a problem with the function below. 我的以下功能有问题。 I can see the log messages as follows: 我可以看到以下日志消息:

in fetchData 在fetchData中
before while 在此之前
in while 在一段时间内
After this, nothing happens until I get a timeout: 在此之后,什么都没有发生,直到我超时:
VM344:1 GET http://192.168.1.91:27080/sds/deception/_find?batch_size=100&criteria= {%20%22$and%22%20:%20[{%20%22$or%22%20:%20[{%20%22reason%22%20:%202%20},%20{%20%22reason%22%20:%203%20}]%20},%20{%20%22ts%22:%20{%20%22$gt%22%20:%20{%20%22$date%22%20:%201549903417360}}}]} VM344:1 GET http://192.168.1.91:27080/sds/deception/_find?batch_size=100&criteria= {%20%22 $ and%22%20:%20 [{%20%22 $ or%22%20 :%20 [{%20%22reason%22%20:%202%20},%20 {%20%22reason%22%20:%203%20}]%20},%20 {%20%22ts% 22:20%{%20%22 $ GT%22%20:%20 {%20%22 $日期%22%20:%201549903417360}}}]}
net::ERR_CONNECTION_TIMED_OUT 网:: ERR_CONNECTION_TIMED_OUT
TypeError: Failed to fetch "Failed to fetch" TypeError:无法获取“无法获取”
after while 过了一阵子

Appreciate some hints to what I do wrong. 赞赏一些提示我做错了什么。

async function fetchData() {
    console.log("in fetchData");
    let page = 0;
    var result = [];
    var mylen;

    var hourAgo = Date.now(Date.now());           // datetime now
    hourAgo = hourAgo - 1000 * 60 * 60 * 24 * 30;   // subtract 30 days
    var myobj = {};
    myobj.ts = String(hourAgo);                     // make unix epoch in ms

    url = `http://192.168.1.91:27080/sds/deception/_find?batch_size=100&criteria={ "$and" : [{ "$or" : [{ "reason" : 2 }, { "reason" : 3 }] }, { "ts": { "$gt" : { "$date" : ${myobj.ts}}}}]}`;
    console.log("before while");
    do {
        console.log("in while");

        try {
            const response = await fetch(url);
            const res_json = await response.json();
            result = result.concat(res_json.results);
            mylen = res_json.results.length;
            if (page++ == 0) {
                url = `http://192.168.1.91:27080/sds/deception/_more?id=${res_json.id}&batch_size=100`;
                console.log(url);
            }
            console.log("Id: ", res_json.id, "\tLen: ", res_json.results.length);
        } catch (err) {
            console.log(err, err.message);
        }
    } while (mylen > 0);

    console.log("after while");

    //console.log(result);
    return result;
}

For one - serialize your JSON 一个-序列化JSON

I observe that your URL is not encoded (URLEncoded) 我发现您的网址未编码(URLEncoded)

 fetchData() async function fetchData() { console.log("in fetchData"); let page = 0; var result = []; var mylen; var hourAgo = Date.now(Date.now()); // datetime now hourAgo = hourAgo - 1000 * 60 * 60 * 24 * 30; // subtract 30 days var myobj = {}; myobj.ts = String(hourAgo); // make unix epoch in ms url = `http://192.168.1.91:27080/sds/deception/_find?batch_size=100&criteria={ "$and" : [{ "$or" : [{ "reason" : 2 }, { "reason" : 3 }] }, { "ts": { "$gt" : { "$date" : ${myobj.ts}}}}]}`; console.log("before while"); do { console.log("in while"); try { console.log("URL being fetched - NOT ENCODED", url) let encodedURL = encodeURI(url); console.log("URL after ENCODING", encodedURL) const response = await fetch(encodedURL); const res_json = await response.json(); result = result.concat(res_json.results); mylen = res_json.results.length; if (page++ == 0) { url = `http://192.168.1.91:27080/sds/deception/_more?id=${res_json.id}&batch_size=100`; console.log(url); } console.log("Id: ", res_json.id, "\\tLen: ", res_json.results.length); } catch (err) { console.log(err, err.message); } } while (mylen > 0); console.log("after while"); //console.log(result); return result; } 

This will help you on URLEncoding 这将帮助您进行URLEncoding

https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_encodeuri https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_encodeuri

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURI

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM