javascript - 如何仅从一个获取请求而不是两个请求中获取响应
问题描述
这是我的代码,我试图从数据库中获取联系人,然后查询每个联系人的 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 INFORMATION_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, function(err, 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, function(err, 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, function(err, 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);
});
});
因此,为了准确地向您展示,这就是我按一次请求后发生的情况。只有我作为蓝图创建的对象数组,将在其上添加数据作为响应发送。 WHereas,当我再次按下 Postman 的请求时,我得到了这个想要的输出。
解决方案
推荐阅读
- sql - 我如何在sql中具有不同列名的多个不同表中一次搜索?
- firebase - 对 Id / Key 的 Firebase 集合组查询
- javascript - 使用 ajax jQuery 在 laravel 中创建时出现内部服务器错误 500,从 ajax 到控制器的变量格式错误
- jquery - 更新模型变量而不刷新页面 ASP.Net MVC RAZOR 页面
- datatables - Angular 7 和 jQuery 数据表触发事件
- java - @Pathvariable 的 UrlMapping 通配符
- html - 即使使用自动检查复选框,如何触发更改
- visual-studio - Visual Studio 2019 编辑器选项卡顺序已更改,而不是按字母顺序?
- javascript - 如何阻止导航栏在html中滚动时缩小
- pandas - Python - 将值从一个数据框传递到另一个查询