我正在尝试为从forEach循环外部声明的变量设置值,但我得到此警告:标识符'arr'从未重新分配;使用'const'代替'let'.tslint(prefer-const)...当我部署代码时,我只会得到一个空变量
import * as functions from "firebase-functions"
import * as admin from "firebase-admin"
admin.initializeApp()
export const test = functions.https.onRequest(async (req, res) => {
const stores = await admin.firestore().collection('stores').get()
let arr: boolean[] = []
// tslint:disable-next-line: no-void-expression
await stores.forEach(async (store) => {
const sales = await store.ref.collection('sales').get()
arr.push(sales.empty)
})
console.log(arr);
res.status(200).send({
arr: arr
})
})
答案 0 :(得分:0)
使用以下代码获得了预期的结果:
export const test = functions.https.onRequest(async (req, res) => {
const fetchStores = await admin.firestore().collection('stores').get()
const arr = []
for (const store of fetchStores.docs) {
const sales = await store.ref.collection('sales').get()
arr.push(sales.empty)
}
res.status(200).send({arr: arr})
})