问题描述
这是我的代码,我试图从数据库中获取联系人,然后查询每个联系人的 messageStore 和 contactStore 以获取每个联系人所需的数据。所以这就是为什么第一个 sql 查询是在 get_info 函数中完成的。 但是,如果在使用 Postman 进行测试时,我必须按两次请求才能获得我想要的正确响应,那么所有代码似乎都可以正常工作。
app.get("/",(req,res) => {
function get_info(data,callback){
var sql = `select LEFT(schema_name,13) AS db_names from @R_61_4045@ION_SCHEMA.SCHEMATA
WHERE SCHEMA_NAME rLIKE '[0-9]w'`;
MysqLConnection.query(sql,function(err,results){
if (err){
throw err;
}
//console.log(results); // good
//stuff_i_want = results; // Scope is larger than function
return callback(results);
});
}
function get_messageCount(id,callback){
var sql = `select count(*) as messageCount from ${id}messageStore.messages`;
MysqLConnection.query(sql,rows){
if (err){
throw err;
}
//console.log(rows); // good
stuff_i_want2 = rows; // Scope is larger than function
return callback(rows);
});
}
function get_contactCount(id,callback){
var sql = `select count(*) as contactCount from ${id}contactStore.wa_contacts`;
MysqLConnection.query(sql,fields){
if (err){
throw err;
}
//console.log(rows); // good
stuff_i_want3 = fields; // Scope is larger than function
return callback(fields);
});
}
function get_dailyMessages(id,callback){
var sql = 'SELECT FROM_UNIXTIME(substr(`timestamp`,1,10),\'%Y %M %D\') AS formattedDate,' +
`COUNT(*) AS receivedMessageCount
FROM ${id}messageStore.messages
GROUP BY formattedDate
order by formattedDate`;
MysqLConnection.query(sql,fields){
if (err){
throw err;
}
//console.log(rows); // good
stuff_i_want4 = fields; // Scope is larger than function
return callback(fields);
});
}
var stuff_i_want = '';
var stuff_i_want2 = '';
var stuff_i_want3 = '';
var stuff_i_want4 = '';
get_info(contacts,function(result){
stuff_i_want = result;
//rest of your code goes in here
for( let i = 0; i < result.length; ++i){
if(!doneOnce) contacts.push(
{
contactNumber: result[i].db_names.replace(/\D/g,""),body: []
}
);
if(i == result.length-1) doneOnce = true;
}
const numbersInfo = JSON.parse(JSON.stringify(stuff_i_want)); //numbersInfo is alphanumerical
let contactsInfo = numbersInfo.map(a => a.db_names.replace(/\D/g,"")); //contactsInfo is changed into numerical only
for( let i =0; i < contactsInfo.length; ++i){
get_messageCount( contactsInfo[i],function(rows){
stuff_i_want2 = rows;
//rest of your code
objIndex = contacts.findindex( (obj => obj.contactNumber == contactsInfo[i]));
contacts[objIndex].body.push(JSON.parse(JSON.stringify(rows)));
});
get_contactCount( contactsInfo[i],function(fields){
stuff_i_want3 = fields;
//rest of your code
objIndex = contacts.findindex( (obj => obj.contactNumber == contactsInfo[i]));
contacts[objIndex].body.push(JSON.parse(JSON.stringify(fields)));
});
get_dailyMessages( contactsInfo[i],function(data){
stuff_i_want4 = data;
//rest of your code
objIndex = contacts.findindex( (obj => obj.contactNumber == contactsInfo[i]));
contacts[objIndex].body.push(JSON.parse(JSON.stringify(data)));
});
//console.log(contacts);
}
res.send(contacts);
});
});
So to show you exactly,this is what happens after i press once on the requests. Only the array of objects i made as a blueprint upon which data would be added is sent as a response. WHereas,when i press the request on Postman again then i get this desired output.
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)