Below is my current working code, but now i need to aggregate the values from 2 separate redis keys into 1 response.
function resolveEnergyProBusDeviceInfo(req: any, res: any) {
const uuid = req.params.uuid
const key = "stream-api-uuid." + uuid + ".devInfo"
client.hgetall(key, function (err, data) {
if (err) {
throw err;
}
if (data !== null) {
const devInfo: devInfo = JSON.parse(JSON.stringify(data))
return res.status(200).json({
"uuid": JSON.parse(String(devInfo.uuid)),
"bus_index": parseInt(String(devInfo.bus_index)),
"sensors": JSON.parse(String(devInfo.sensor))
});
} else {
return res.status(204).send();
}
});
}
so i'm trying to do something like the below (apologies im still in the synchronous mindset):
function resolveEnergyProBusDeviceInfo2(req: any, res: any) {
const uuid = req.params.uuid
const key0 = "stream-api-uuid." + uuid + ".0.devInfo"
const key1 = "stream-api-uuid." + uuid + ".1.devInfo"
const bus0Info = queryDevInfoForIndex(key0);
const bus1Info = queryDevInfoForIndex(key1);
return res.status(200).json({
"uuid": uuid,
"bus_info0": JSON.parse(String(bus0Info)),
"bus_info1": JSON.parse(String(bus1Info))
});
}
function queryDevInfoForIndex(key: string) {
client.hgetall(key, function (err, data) {
if (err) {
throw err;
}
if (data !== null) {
const busInfo: devInfo = JSON.parse(JSON.stringify(data))
return busInfo;
} else
const busInfo: devInfo = JSON.parse(JSON.stringify(data))
return busInfo;
})
}
Because hgetall returns a boolean, and it's data can seemingly only be manipulated within its callback, im having trouble constructing the response on a higher level. Was looking into promises but didn't get too far yet...