Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js-为什么读取属性';长度';未定义的?_Node.js_Api_Google Sheets - Fatal编程技术网

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]
是一个数组。我的观点是,如果看不到数据,就无法知道这些数据是什么。你似乎对数据有所了解,但显然,这只是一个猜测。