如何在 google 主页操作中使用请求承诺进行异步调用
how to make async call using request-promise in google home action
我正在尝试在 onSync() 和 return 有效负载中调用 API,以便我将获得设备数量。即使 api 也在 return 向我发送我无法显示设备的正确数据。以下是代码片段。
app.onSync((body) => {
// TODO: Implement SYNC response
console.log('************** inside sync **************');
var payload = {
agentUserId:'123',
devices:[]
};
//Calling Discove Devices API
requestAPI('------ calling API ------')
.then(function(data){
let result = JSON.parse(data);
console.log('********** RESULT ********** '+util.inspect(result,{depth:null}));
var count = 0;
count = Object.keys(result.Devices).length;
console.log('********** DEVICE COUNT ********** '+count);
//forming payload json of devices
for(var i in result.Devices){
var item = result.Devices[i];
payload.devices.push({
"id" : item.ieee_address,
"type" : 'action.devices.types.OUTLET',
"traits" : ['action.devices.traits.OnOff'],
name : {
"defaultNames" : [item.mapped_load],
"name" : item.mapped_load,
"nicknames" : [item.mapped_load],
},
"willReportState" : false,
"deviceInfo" : {
"manufacturer" : 'some manufacturer',
"model" : 'Smart-Plug',
"hwVersion" : '1.0',
"swVersion" : '1.0.1',
},
});
}
}).catch(function(err){
console.log(err);
});
console.log('PAYLOAD %J ',payload); <----- it is always empty
return {
requestId: body.requestId,
payload: payload,
};
});
API return 给我正确的值,但有效负载始终为空。
请帮忙。我是 node.js 的新手,我不知道如何进行异步调用。
您正在使用异步调用来获取设备,并且您需要确保在请求完成之前没有 return 数据。你 return 函数的 Promise,所以它会等待:
app.onSync((body) => {
return requestApi('...')
.then(data => {
return {
requestId: body.requestId,
payload: payload
}
})
})
我正在尝试在 onSync() 和 return 有效负载中调用 API,以便我将获得设备数量。即使 api 也在 return 向我发送我无法显示设备的正确数据。以下是代码片段。
app.onSync((body) => {
// TODO: Implement SYNC response
console.log('************** inside sync **************');
var payload = {
agentUserId:'123',
devices:[]
};
//Calling Discove Devices API
requestAPI('------ calling API ------')
.then(function(data){
let result = JSON.parse(data);
console.log('********** RESULT ********** '+util.inspect(result,{depth:null}));
var count = 0;
count = Object.keys(result.Devices).length;
console.log('********** DEVICE COUNT ********** '+count);
//forming payload json of devices
for(var i in result.Devices){
var item = result.Devices[i];
payload.devices.push({
"id" : item.ieee_address,
"type" : 'action.devices.types.OUTLET',
"traits" : ['action.devices.traits.OnOff'],
name : {
"defaultNames" : [item.mapped_load],
"name" : item.mapped_load,
"nicknames" : [item.mapped_load],
},
"willReportState" : false,
"deviceInfo" : {
"manufacturer" : 'some manufacturer',
"model" : 'Smart-Plug',
"hwVersion" : '1.0',
"swVersion" : '1.0.1',
},
});
}
}).catch(function(err){
console.log(err);
});
console.log('PAYLOAD %J ',payload); <----- it is always empty
return {
requestId: body.requestId,
payload: payload,
};
});
API return 给我正确的值,但有效负载始终为空。 请帮忙。我是 node.js 的新手,我不知道如何进行异步调用。
您正在使用异步调用来获取设备,并且您需要确保在请求完成之前没有 return 数据。你 return 函数的 Promise,所以它会等待:
app.onSync((body) => {
return requestApi('...')
.then(data => {
return {
requestId: body.requestId,
payload: payload
}
})
})