- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在进行 API 调用以使所有设备连接到我的本地服务器。然后,我尝试循环所有连接的设备并进行更多调用以获取一些数据。我如何循环遍历所有设备而不仅仅是数组中的第一个设备?
这是我存储在设备变量中的响应
[{"connected":true,"functions":["SetGain","SetIntegTime","SetInterrupt","Enable","begin"],"id":"1f0039000547363339343638","last_app":null,"last_heard":"2018-10-26T16:01:34.974Z","last_ip_address":"::ffff:10.10.10.5","name":"actually-jail","platform_id":6,"product_firmware_version":65535,"product_id":6,"status":"normal","variables":{"RawData":"string","ColorTemp":"string","Lux":"string","SensorArray":"string","Gain":"string","IntegraTime":"string","TemperatureC":"double","TemperatureF":"double","TemperatureK":"double","TemperatureR":"double"}},{"connected":false,"functions":null,"id":"3a003a001047363333343437","last_app":null,"status":"normal","variables":null}]
async function getDevices() {
let devices = await axios('http://10.10.10.1:8080/v1/devices/?access_token=687b5aee0b82f653c725570422dea5d37de6b65f')
return this.devices = (devices.data[0].id)};
async function getVariables() {
try {
const devices = await getDevices();
console.log(devices);
const RawDataPromise = axios(API + devices + '/RawData/?access_token=' + Token);
const ColorTempPromise = axios(API + devices + '/ColorTemp/?access_token=' + Token);
const LuxPromise = axios(API + devices + '/Lux/?access_token=' + Token);
const SensorArrayPromise = axios(API + devices + '/SensorArray/?access_token=' + Token);
const GainPromise = axios(API + devices + '/Gain/?access_token=' + Token);
const IntegraTimePromise = axios(API + devices + '/IntegraTime/?access_token=' + Token);
const TemperatureCPromise = axios(API + devices + '/TemperatureC/?access_token=' + Token);
const TemperatureFPromise = axios(API + devices + '/TemperatureF/?access_token=' + Token);
const TemperatureKPromise = axios(API + devices + '/TemperatureK/?access_token=' + Token);
const TemperatureRPromise = axios(API + devices + '/TemperatureR/?access_token=' + Token);
const [RawData, ColorTemp, Lux, SensorArray, Gain, IntegraTime, TemperatureC, TemperatureF, TemperatureK, TemperatureR] = await Promise.all([RawDataPromise, ColorTempPromise, LuxPromise, SensorArrayPromise,GainPromise,IntegraTimePromise,TemperatureCPromise,TemperatureFPromise,TemperatureKPromise,TemperatureRPromise]);
console.log(RawData.data.result, ColorTemp.data.result,Lux.data.result,SensorArray.data.result,Gain.data.result,IntegraTime.data.result,TemperatureC.data.result,TemperatureF.data.result,TemperatureK.data.result,TemperatureR.data.result);
} catch (e) {
console.error(e)
}
}
getVariables();
最佳答案
创建一个包装函数以从单个设备获取值,然后使用 Promise.all(如果使用 bluebird,则使用 Promise.map/each)多次调用该函数;
async function getDevices() {
let devices = await axios('http://10.10.10.1:8080/v1/devices/?access_token=687b5aee0b82f653c725570422dea5d37de6b65f');
return this.devices = (devices.data)
};
async function getForOneDevice(device) {
// I've assumed here that 'devices' in your API calls was a typo, and was intended to be devices[0] (based on the question)
const RawDataPromise = axios(API + device + '/RawData/?access_token=' + Token);
const ColorTempPromise = axios(API + device + '/ColorTemp/?access_token=' + Token);
const LuxPromise = axios(API + device + '/Lux/?access_token=' + Token);
const SensorArrayPromise = axios(API + device + '/SensorArray/?access_token=' + Token);
const GainPromise = axios(API + devices + '/Gain/?access_token=' + Token);
const IntegraTimePromise = axios(API + device + '/IntegraTime/?access_token=' + Token);
const TemperatureCPromise = axios(API + device + '/TemperatureC/?access_token=' + Token);
const TemperatureFPromise = axios(API + device + '/TemperatureF/?access_token=' + Token);
const TemperatureKPromise = axios(API + device + '/TemperatureK/?access_token=' + Token);
const TemperatureRPromise = axios(API + device + '/TemperatureR/?access_token=' + Token);
return Promise.all([RawDataPromise, ColorTempPromise, LuxPromise, SensorArrayPromise,GainPromise,IntegraTimePromise,TemperatureCPromise,TemperatureFPromise,TemperatureKPromise,TemperatureRPromise]);
}
async function getVariables() {
try {
const devices = await getDevices();
console.log(devices);
const promises = [];
devices.forEach((device) => {
promises.push(getForOneDevice(device.id));
})
const allResults = Promise.all(promises);
} catch (e) {
console.error(e)
}
}
getVariables();
关于javascript - 循环遍历数组以进行 axios 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53011917/
我是一名优秀的程序员,十分优秀!