简体   繁体   中英

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

I made a break from my react native project for about 3 weeks and now I came back and wanted to work on it, unfortunaly the app doesnt even render the first screen components. I already figured out what the problem is, Im just totaly confused what is going on:

    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]);
        };
    });

The problem is located in the for-loop. Here is the error Im receiving:

[Unhandled promise rejection: ReferenceError: Can't find variable: i]

  • node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in 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 in _callImmediatesPass
  • node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
  • [native code]:null in flushedQueue
  • [native code]:null in callFunctionReturnFlushedQueue

How can JS not find the variable "i"? I never told JS to search for "i" - it's just supposed to be a counter to loop through the whole snapshot.val() array. What is going on here?

The error states that i is not defined. reading your code, I can see that i inside your for loop declaration may be missing the let i variable.

Have you tried for (let i in snapshot.val()){

i is not declared in the scope. Try

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

OR

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

I made a break from my react native project for about 3 weeks and now I came back and wanted to work on it, unfortunaly the app doesnt even render the first screen components. I already figured out what the problem is, Im just totaly confused what is going on:

    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]);
        };
    });

The problem is located in the for-loop. Here is the error Im receiving:

[Unhandled promise rejection: ReferenceError: Can't find variable: i]

  • node_modules\react-native\node_modules\promise\setimmediate\core.js:37:13 in 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 in _callImmediatesPass
  • node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard
  • node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
  • [native code]:null in flushedQueue
  • [native code]:null in callFunctionReturnFlushedQueue

How can JS not find the variable "i"? I never told JS to search for "i" - it's just supposed to be a counter to loop through the whole snapshot.val() array. What is going on here?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM