javascript - 在每个循环 Node js 中使用请求模块不会更新我的 JSON

标签 javascript json node.js

这段代码可能有什么问题?我无法在 forEach 循环后获得更新的 json(变量 updatedDataJSON)输出,有人可以帮我吗?预先感谢,这是我的代码,我在这里遗漏了什么吗?

app.get('/getDifferences', (req, res) => {
    let changed = false,
        updatedJSON = { data: [] },
        updatedDataJSON = { "url": "", "dta": "", "status": "unchanged" };
    fs.readFile('output.json', 'utf-8', (err, data) => {
        storedData = data && JSON.parse(data);
        if (storedData) {
            storedData.data.forEach((e) => {
                request.get(e.url).on('res', (html) => {
                    updatedDataJSON.url = e.url;
                    updatedDataJSON.dta = e.dta;

                    if (e.dta && (e.dta.match(/<body[^>]*>[\s\S]*<\/body>/gi)[0] !== html.match(/<body[^>]*>[\s\S]*<\/body>/gi)[0])) {
                        changed = true;
                        updatedDataJSON.status = "changed";
                        updatedDataJSON.dta = html;
                    }
                    updatedJSON.data.push(updatedDataJSON);
                });
            });
            console.log(updatedDataJSON);
        }
    });
    res.end(JSON.stringify(updatedJSON))
});

最佳答案

Node js 是异步的。所以当get请求将被调用storedData.data length时间时,你必须在forEach循环中的get请求的callBack中结束响应。

这里我附上代码来演示这一点。

    app.get('/getDifferences', (req, res) => {
    let changed = false,
        updatedJSON = { data: [] },
        updatedDataJSON = { "url": "", "dta": "", "status": "unchanged" };
    fs.readFile('output.json', 'utf-8', (err, data) => {
        var count = 0,
            arraylength;
        storedData = data && JSON.parse(data);
        if (storedData) {
            arraylength = storedData.data;
            storedData.data.forEach((e) => {
                request.get(e.url).on('res', (html) => {
                    updatedDataJSON.url = e.url;
                    updatedDataJSON.dta = e.dta;

                    if (e.dta && (e.dta.match(/<body[^>]*>[\s\S]*<\/body>/gi)[0] !== html.match(/<body[^>]*>[\s\S]*<\/body>/gi)[0])) {
                        changed = true;
                        updatedDataJSON.status = "changed";
                        updatedDataJSON.dta = html;
                    }
                    updatedJSON.data.push(updatedDataJSON);
                    count++;
                    if (count === arraylength) {
                        console.log(updatedDataJSON);
                        res.end(JSON.stringify(updatedJSON));
                    }
                });
            });
        }
    });
});

关于javascript - 在每个循环 Node js 中使用请求模块不会更新我的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48421196/

相关文章:

javascript - 如何将嵌套对象拆分为对象数组 - 避免 for 循环

node.js - 使用 ntwitter 在 Node.js 中多次调用 Twitter 流 API

javascript - 使用 YQL 查询进行 HTML 和 JS 调试

javascript - 设置日期的年、月、日、时、分、秒和 UTC 偏移值

javascript - 将 jquery 变量合并到一个选择器中?

javascript - 如何让 jQuery DataTables 在多个 DOM 表之间切换?

javascript - NodeJS & Gulp Streams & Vinyl File Objects - Gulp Wrapper for NPM package producing incorrect output

javascript - 构建此 JSON 数据的更好方法?

java - Jackson @JsonProperty(required=true) 不会抛出异常

javascript - 在 Node 和浏览器中使用 require.js 模块