gpt4 book ai didi

javascript - 从循环nodejs中的函数获取值

转载 作者:行者123 更新时间:2023-11-29 18:30:11 25 4
gpt4 key购买 nike

我是 javascript 和 Node js 的新手。我其实想根据MySQL值来比较两个数据。

我想要做的是从db -> concat数据对和value -> endloopdata -> get值循环

这是我的代码

routes/masterdata.js

var Masterdata = require('../models/Masterdata');
var outputFile = {}
for (var i = 0; i < dataFile.length; i++) {
if (dataFile[i]['existing'] != null) {
for (var x = 0; x < dataFile.length; x++) {
var param = {
tech_row: dataFile[i]['existing'],
tech_col: dataFile[x]['new']
};

Masterdata.compareData(param, function(err, rows) {
console.log(rows);
outputFile.push({
value: rows
});
});
}
}
}

console.log(outputFile);

models/Masterdata.js

var Masterdata = {
compareData: function(param, callback) {
return db.query('SELECT value FROM sod_matrix WHERE TECH_NAME_ROW = ? AND TECH_NAME_COL = ?', [param.tech_row, param.tech_col], callback);

}
}

我的问题是如何将函数比较数据中的数据填充到循环中的数组中?

最佳答案

var Promise = require('bluebird');
var completed=[];
var Masterdata = require('../models/Masterdata');
var outputFile = []; //should be an array
for (var i = 0; i < dataFile.length; i++) {
if (dataFile[i]['existing'] != null) {
for (var x = 0; x < dataFile.length; x++) {
var param = {
tech_row: dataFile[i]['existing'],
tech_col: dataFile[x]['new']
};
completed.push(new Promise(resolve, reject){
Masterdata.compareData(param, function(err, rows) {
if(err)
{
reject(err);
}
else
{
console.log(rows);
outputFile.push(rows); //that;s pretty enough
resolve();
}
});
});
}
}
}
Promise.all(completed)
.then((res)
{
console.log(outputFile);
});

关于javascript - 从循环nodejs中的函数获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45752920/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com