繁体   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