英文:
Why I can get my Nest thermostat ID but not other people using the same script?
问题
我有一个在脚本中的listDevice函数,用于获取恒温器ID(与此处相同):
function listDevices() {
// 指定端点
const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
// 空数组用于保存设备数据
let deviceArray = [];
// 发送请求到智能API
const data = makeRequest(endpoint);
const deviceData = data.devices;
console.log(deviceData);
deviceData.forEach(device => {
const name = device.name;
const type = device.type;
deviceArray.push([name,type]);
});
// 获取表格
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
// 输出数据
sheet.getRange(2,1,deviceArray.length,2).setValues(deviceArray);
}
它对我的Nest起作用,但是当尝试使其适用于具有多个Nest恒温器的其他人时,会出现此错误:
TypeError: Cannot read properties of undefined (reading 'forEach')
您知道为什么此脚本对我有效,但与其他人共享时会出现错误吗?
这是上面函数调用的makeRequest(endpoint)函数:
function makeRequest(endpoint) {
// 获取智能服务
const smartService = getSmartService();
// 获取访问令牌
const access_token = smartService.getAccessToken();
//console.log(access_token);
// 设置SMD API网址
const url = 'https://smartdevicemanagement.googleapis.com/v1';
//const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
// 设置调用的标头
const headers = {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
}
// 设置参数
const params = {
'headers': headers,
'method': 'get',
'muteHttpExceptions': true
}
// 尝试调用API
try {
const response = UrlFetchApp.fetch(url + endpoint, params);
const responseBody = JSON.parse(response.getContentText());
return responseBody;
}
catch(e) {
console.log('Error: ' + e);
}
}
英文:
I have that listDevice function in a script to get the thermostat ID (same as here):
function listDevices() {
// specify the endpoint
const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
// blank array to hold device data
let deviceArray = [];
// make request to smart api
const data = makeRequest(endpoint);
const deviceData = data.devices;
console.log(deviceData);
deviceData.forEach(device => {
const name = device.name;
const type = device.type;
deviceArray.push([name,type]);
});
// get the Sheet
const ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
// output the data
sheet.getRange(2,1,deviceArray.length,2).setValues(deviceArray);
}
It works fine with my Nest, but when trying to make it work for another person with multiple Nest thermostats, it gives this error:
> TypeError: Cannot read properties of undefined (reading 'forEach')
Would you know why this script works for me, but when shared with someone it gives an error?
Here's the makeRequest(endpoint) function that is called by the function above:
function makeRequest(endpoint) {
// get the smart service
const smartService = getSmartService();
// get the access token
const access_token = smartService.getAccessToken();
//console.log(access_token);
// setup the SMD API url
const url = 'https://smartdevicemanagement.googleapis.com/v1';
//const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
// setup the headers for the call
const headers = {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
}
// set up params
const params = {
'headers': headers,
'method': 'get',
'muteHttpExceptions': true
}
// try calling API
try {
const response = UrlFetchApp.fetch(url + endpoint, params);
const responseBody = JSON.parse(response.getContentText());
return responseBody;
}
catch(e) {
console.log('Error: ' + e);
}
}
答案1
得分: 0
在将makeRequest(endpoint)
函数替换为这个函数后,脚本可以正常工作,并且可以在不再运行listDevice
函数的情况下提供设备的ID:
function makeRequesttest() {
// 获取智能服务
const smartService = getSmartService();
// 获取访问令牌
const access_token = smartService.getAccessToken();
//console.log(access_token);
// 设置SMD API的URL
const url = 'https://smartdevicemanagement.googleapis.com/v1';
const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
// 设置调用的标头
const headers = {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
}
// 设置参数
const params = {
'headers': headers,
'method': 'get',
'muteHttpExceptions': true
}
// 尝试调用API
try {
const response = UrlFetchApp.fetch(url + endpoint, params);
const responseBody = JSON.parse(response.getContentText());
Logger.log('response: ' + response);
return responseBody;
}
catch(e) {
console.log('Error: ' + e);
//throw e;
}
}
英文:
After replacing the makeRequest(endpoint) function with that one, the script works and provides me the devices ID without needing to run the listDevice function anymore:
function makeRequesttest() {
// get the smart service
const smartService = getSmartService();
// get the access token
const access_token = smartService.getAccessToken();
//console.log(access_token);
// setup the SMD API url
const url = 'https://smartdevicemanagement.googleapis.com/v1';
const endpoint = '/enterprises/' + PROJECT_ID + '/devices';
// setup the headers for the call
const headers = {
'Authorization': 'Bearer ' + access_token,
'Content-Type': 'application/json'
}
// set up params
const params = {
'headers': headers,
'method': 'get',
'muteHttpExceptions': true
}
// try calling API
try {
const response = UrlFetchApp.fetch(url + endpoint, params);
const responseBody = JSON.parse(response.getContentText());
Logger.log('response: ' + response);
return responseBody;
}
catch(e) {
console.log('Error: ' + e);
//throw e;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论