[英]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.我从我的 React Native 项目中休息了大约 3 周,现在我回来并想继续它,不幸的是应用程序甚至没有呈现第一个屏幕组件。 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.问题出在for循环中。 Here is the error Im receiving:这是我收到的错误:
[Unhandled promise rejection: ReferenceError: Can't find variable: i] [未处理的 promise 拒绝:ReferenceError:找不到变量: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:37:13 在 tryCallOne
- node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0 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: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:181:14 在_callImmediatesPass
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates 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 __callImmediates 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 在 __guard$argument_0
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard __guard 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue
- [native code]:null in flushedQueue [本机代码]:flushedQueue中的null
- [native code]:null in callFunctionReturnFlushedQueue [native code]:null in callFunctionReturnFlushedQueue
How can JS not find the variable "i"? JS怎么找不到变量“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.我从未告诉 JS 搜索“i”——它只是应该是一个遍历整个 snapshot.val() 数组的计数器。 What is going on here?这里发生了什么?
The error states that i
is not defined.错误指出未定义i
。 reading your code, I can see that i
inside your for loop declaration may be missing the let i
variable.阅读您的代码,我可以看到您的 for 循环声明中的i
可能缺少let i
变量。
Have you tried for (let i in snapshot.val()){
你有没有试过for (let i in snapshot.val()){
i
is not declared in the scope. Try i
没有在 scope 中声明。试试
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.我从我的 react native 项目中休息了大约 3 周,现在我回来并想继续工作,不幸的是,该应用程序甚至没有渲染第一个屏幕组件。 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.问题出在for循环中。 Here is the error Im receiving:这是我收到的错误:
[Unhandled promise rejection: ReferenceError: Can't find variable: i] [未处理的 promise 拒绝:ReferenceError:找不到变量: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:37:13 在 tryCallOne
- node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 in setImmediate$argument_0 node_modules\react-native\node_modules\promise\setimmediate\core.js:123:24 在 setImmediate$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14 in _callTimer _callTimer 中的 node_modules\react-native\Libraries\Core\Timers\JSTimers.js:130:14
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14 in _callImmediatesPass _callImmediatesPass 中的 node_modules\react-native\Libraries\Core\Timers\JSTimers.js:181:14
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 in callImmediates node_modules\react-native\Libraries\Core\Timers\JSTimers.js:441:30 在 callImmediates
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6 in __callImmediates __callImmediates 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:387:6
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6 in __guard$argument_0 __guard$argument_0 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:135:6
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10 in __guard __guard 中的 node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:364:10
- node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 in flushedQueue node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:134:4 在flushedQueue
- [native code]:null in flushedQueue 【原生代码】:flushedQueue中的null
- [native code]:null in callFunctionReturnFlushedQueue 【原生代码】:null in callFunctionReturnFlushedQueue
How can JS not find the variable "i"? JS怎么找不到变量“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.我从来没有告诉 JS 搜索“i”——它应该是一个循环遍历整个 snapshot.val() 数组的计数器。 What is going on here?这里发生了什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.