ReplaceAll导致array.reduce中的问题

时间:2018-02-15 14:50:01

标签: javascript arrays promise

我还是很陌生,所以如果我不这样做,请原谅我。说得对。我们有一个array.reduce,它调用一个带有返回promise的方法,该方法遍历文件列表并将结果发布到db。一切都工作得很好,直到它遇到一个有撇号的字段然后数据库插入失败。这是字段值。 '预计100002822'是100002822。'

我尝试在字段上添加replaceAll,现在在array.reduce中出现错误。

这是.reduce



console.log('Found test results in ' + matches.length + ' files. Parsing and posting to the database now...');
                        var startTime = moment();
                        var parser = new Parser();
                        matches.reduce(function (p, val) {
                            return p.then(function () {
                                return parser.parseResults(val);
                            });
                        }, Promise.resolve()).then(function (finalResult) {

                            var endTime = moment();
                            var testDuration = moment.duration(endTime.diff(startTime));
                            console.log(chalk.blue('*** File parsing time: ' + testDuration.humanize() + ' ***'));
                            if (finalResult.insertSuccess == matches.length) {

                                var publishOut = {
                                    totalFiles: matches.length,
                                    totalTests: 0,
                                    totalTestsSuccess: 0,
                                    totalTestsFailed: 0
                                }
                                publishOut.totalTests += finalResult.totalTests;
                                publishOut.totalTestsSuccess += finalResult.testPassedCount;
                                publishOut.totalTestsFailed += finalResult.testFailedCount;
                                console.log(`Successfully inserted ${finalResult.insertSuccess} of ${publishOut.totalTests} test results.`);

                                // for (var i = 0; i < matches.length; i++) {
                                //     var currentFile = `./testing/results/${matches[i]}`;
                                //     fs.unlinkSync(currentFile);
                                // }

                                resolve(publishOut);

                            } else {
                                reject('Only ' + finalResult.insertSuccess + ' of ' + matches.length + ' successfully posted to the database');
                            }

                        }, function (err) {
                            reject('error in reduce', err);
                        });
&#13;
&#13;
&#13;

我尝试过几种不同的方法来使用具有相同故障的replaceAll。它从array.reduce

中获取此代码

&#13;
&#13;
}, function (err) {
   reject('error in reduce', err);
});
&#13;
&#13;
&#13;

这是被调用的方法。导致.reduce失败的添加代码是Message = expectation.message.replaceAll(&#34;&#39;&#34;,&#34;&#34;);

&#13;
&#13;
protractorParser.prototype.parseResults = function (fileName) {
    return new Promise((resolve, reject) => {
        //console.log('In parseresults', fileName);
        var currentFile = './testing/results/' + fileName
        json.readFile(currentFile, function (err, obj) {
            if (err != null) {
                console.log('error reading file', err);
                reject(err);
            } else {
                resolve(obj);
            }
        });
    }).then(function (obj) {
        var results = [];


        for (var suite in obj) {
            var specs = obj[suite].specs;
            for (let i = 0; i < specs.length; i++) {
                const assert = specs[i];
                const tcR = /TC[\d]+/;
                const tc = assert.description.match(tcR);

                let Passed = 1;
                let Message = '';
                let Stack = '';

                testResults.totalTests++;
                
                if (assert.failedExpectations.length) {
                    const expectation = assert.failedExpectations[assert.failedExpectations.length - 1];
                    Passed = 0;
                    Message = expectation.message.replaceAll("'", "");
                    Stack = expectation.stack.split('\n')[1].trim();
                    testResults.testFailedCount++
                } else {
                    testResults.testPassedCount++
                }
                if (tc != null) {

                    const time = moment().utcOffset(config.get('settings.timeOffset')).format('YYYY-MM-DDTHH:mm:ss');
                    const promise = utility.TestDataManager.insertAutomationResults(tc[0], assert.description, Passed, process.env.testBuild, 'P', Message, Stack, 0, time, '');
                    results.push(promise.then(() => {

                        //fs.unlinkSync(currentFile);
                        testResults.insertSuccess++;
                        //console.log('insertSuccess', testResults.insertSuccess);

                    },
                        err => { console.log('… failed', err); throw err; }
                    ));

                } else {
                    console.log('no test case found for test: ' + assert.description + ' -- skipping');
                    // I don't think you want to `throw err` here, right?
                }
            }
        }
        return Promise.all(results).then(() => testResults);
    });
};
&#13;
&#13;
&#13;

0 个答案:

没有答案