簡體   English   中英

ReferenceError: Can't find variable: i (for循環問題)

[英]ReferenceError: Can't find variable: i (for-loop problem)

我從我的 React Native 項目中休息了大約 3 周,現在我回來並想繼續它,不幸的是應用程序甚至沒有呈現第一個屏幕組件。 我已經弄清楚問題出在哪里,我只是完全弄糊塗了:

    var chatkeys = [];
    var partnerkeys = [];
    var contactsDATA = [];

    await firebase.database().
    ref(`users/${PersonalId}/chats`).
    once('value').
    then(snapshot =>{
        for (i in snapshot.val()){//pulling all chatcodes out of database and pushing into chatkeys
            chatkeys.push(snapshot.val()[i]);
        };
    });

問題出在for循環中。 這是我收到的錯誤:

[未處理的 promise 拒絕:ReferenceError:找不到變量:i]

  • node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 在 tryCallOne
  • node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0
  • node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer
  • node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 在_callImmediatesPass
  • node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
  • __callImmediates 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 在 __guard$argument_0
  • __guard 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
  • [本機代碼]:flushedQueue中的null
  • [native code]:null in callFunctionReturnFlushedQueue

JS怎么找不到變量“i”? 我從未告訴 JS 搜索“i”——它只是應該是一個遍歷整個 snapshot.val() 數組的計數器。 這里發生了什么?

錯誤指出未定義i 閱讀您的代碼,我可以看到您的 for 循環聲明中的i可能缺少let i變量。

你有沒有試過for (let i in snapshot.val()){

i沒有在 scope 中聲明。試試

for (let i in snapshot.val()){//pulling all chatcodes out of database and pushing into chatkeys
            chatkeys.push(snapshot.val()[i]);
        };

要么

let i;
for (i in snapshot.val()){//pulling all chatcodes out of database and pushing into chatkeys
            chatkeys.push(snapshot.val()[i]);
        };

我從我的 react native 項目中休息了大約 3 周,現在我回來並想繼續工作,不幸的是,該應用程序甚至沒有渲染第一個屏幕組件。 我已經弄清楚問題是什么,我只是完全混淆了發生了什么:

    var chatkeys = [];
    var partnerkeys = [];
    var contactsDATA = [];

    await firebase.database().
    ref(`users/${PersonalId}/chats`).
    once('value').
    then(snapshot =>{
        for (i in snapshot.val()){//pulling all chatcodes out of database and pushing into chatkeys
            chatkeys.push(snapshot.val()[i]);
        };
    });

問題出在for循環中。 這是我收到的錯誤:

[未處理的 promise 拒絕:ReferenceError:找不到變量:i]

  • node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 在 tryCallOne
  • node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 在 setImmediate$argument_0
  • _callTimer 中的 node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14
  • _callImmediatesPass 中的 node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14
  • node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 在 callImmediates
  • __callImmediates 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6
  • __guard$argument_0 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6
  • __guard 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 在flushedQueue
  • 【原生代碼】:flushedQueue中的null
  • 【原生代碼】:null in callFunctionReturnFlushedQueue

JS怎么找不到變量“i”? 我從來沒有告訴 JS 搜索“i”——它應該是一個循環遍歷整個 snapshot.val() 數組的計數器。 這里發生了什么?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM