Node.js-为什么读取属性';长度';未定义的?
这是我的密码。 我正在写一个链接到谷歌电子表格的程序。这就是它的问题阅读功能Node.js-为什么读取属性';长度';未定义的?,node.js,api,google-sheets,Node.js,Api,Google Sheets,这是我的密码。 我正在写一个链接到谷歌电子表格的程序。这就是它的问题阅读功能 function getQuestions() { var sheets = google.sheets('v4'); sheets.spreadsheets.values.get({ auth: oauth2Client, spreadsheetId: mySheetId, range:encodeURI('123'), }, function(err, response)
function getQuestions() {
var sheets = google.sheets('v4');
sheets.spreadsheets.values.get({
auth: oauth2Client,
spreadsheetId: mySheetId,
range:encodeURI('123'),
}, function(err, response) {
if (err) {
console.log('The API for reading the problem file caused a problem:' + err);
return;
}
const rows = response.values;
if (rows.length == 0) {
console.log('No data found.');
} else {
myQuestions=rows;
totalSteps=myQuestions[0].length;
console.log('The questions to ask have been downloaded!');
}
});
}
但是当我执行它时,它会发生异常
(node:7380) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'length' of undefined
at C:\Users\User\Desktop\Bottest(Node.js)\index.js:53:15
at C:\Users\User\Desktop\Bottest(Node.js)\node_modules\googleapis-common\build\src\apirequest.js:48:53
at processTicksAndRejections (internal/process/task_queues.js:95:5)
为什么会这样?
对不起,是不是传递的谷歌电子表格根本不是数组?
谷歌似乎对其电子表格进行了更新,数据类型也发生了变化。但我不知道当前表中传递的数据类型是什么…
myQuestions[0]
不是一个数组。因此,您将收到一个错误:无法读取未定义的属性“length”
使用console.log()
进行调试以找出问题。快乐编码:)行
或我的问题[0]
是未定义的
。您必须进行调试才能找到原因。作为一种简单的调试方法,我建议您在totalSteps=myQuestions[0]之前使用console.log(myQuestions)
代码>。它可能不是一个数组,而是一个包含数组的对象,或者可能是一个必须解析的JSON字符串(JSON.parse(myQuestions)
)。请尝试:if(!rows?.length | |!rows[0]?.length){//未找到数据
您是如何确定行
是数组的?行。长度==0
此行表示行是数组。您也可以通过该变量的名称来假定。myQuestions[0]。长度
同样表示myQuestions[0]
是一个数组。我的观点是,如果看不到数据,就无法知道这些数据是什么。你似乎对数据有所了解,但显然,这只是一个猜测。