NodeJS 使用循环内回调修改回调外变量的值

wz1wpwve  于 2023-01-08  发布在  Node.js
关注(0)|答案(1)|浏览(188)

我是Nodejs的新手,我面临着一个问题:用循环内的回调修改回调外的变量值。
我正在编写在线判断项目,这是我的功能,检查输出的程序与答案从数据库。我创建了结果对象,以存储数量的正确测试用例。

function compareResult(fileName, problem, timeLimit, callback) {
        const cp = require('child_process');
        const exePath = 'submit\\' + fileName + '.exe';
        const child = cp.spawn(exePath, ['--from=markdown', '--to=html'], {timeout: timeLimit});
        MongoClient.connect(uri, function(err, db) {
            if (err) throw err;
            var dbo = db.db(dbName);
            var query = { id_problem: problem, is_eg: "false" };
            var proj = { projection: {input: 1, output: 1} };
            dbo.collection("sample").find(query, proj).toArray(function(err, arr) {
                if (err) throw err;
                if (arr != null) {

                    var result = {
                        correct: 0,
                        total: arr.length
                    };

                    for (const json of arr) {
                        const answer = json['output'];
                        child.stdin.write(json['input']);
                        child.stdout.on('data', function(data) {
                            if (data == answer) { 
                                result.correct += 1; // I want to modify result object here.
                            }
                        });
                        child.stdin.end();
                    };
                    console.log(result);
                    callback(result);
                }
            });
        });

我想在那个地方修改结果对象,怎么做?

wh6knrhe

wh6knrhe1#

function compareResult(fileName, problem, timeLimit, callback) {
    const cp = require('child_process');
    const exePath = 'submit\\' + fileName + '.exe';
    const child = cp.spawn(exePath, ['--from=markdown', '--to=html'], {timeout: timeLimit});
    MongoClient.connect(uri, function(err, db) {
        if (err) throw err;
        var dbo = db.db(dbName);
        var query = { id_problem: problem, is_eg: "false" };
        var proj = { projection: {input: 1, output: 1} };
        dbo.collection("sample").find(query, proj).toArray(function(err, arr) {
            if (err) throw err;
            if (arr != null) {

                var result = {
                    correct: 0,
                    total: arr.length
                };

                for (const json of arr) {
                    const answer = json['output'];
                    child.stdin.write(json['input']);
                    child.stdout.on('data', function(data) {
                        if (data == answer) { 
                            result.correct += 1; 
                        }
                        // Decrement total here to track how many 'data' events have been emitted
                        result.total--; 
                        if (result.total === 0) {
                            // All 'data' events have been emitted, so call the callback function
                            callback(result);
                        }
                    });
                    child.stdin.end();
                };
            }
        });
    });
}

相关问题