niklodeon
niklodeon

Reputation: 1380

Typescript Cloud function not compiling

I have following cloud function to get data from the firebase datastore but it doesn't compile and am not sure how to fix this.

import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';

admin.initializeApp(functions.config().firebase);

const db = admin.firestore();
const usersObj = db.collection('users')

export const getUsers = async () => {
    let allUsers: Array<any> = [];
    await usersObj.get().then(users => {
        users.forEach(user => {
            allUsers[user.id] = user.data();
        });
    });
    return allUsers;
}

error: error TS7015: Element implicitly has an 'any' type because index expression is not of type 'number'. allOrders[order.id] = order.data();

Upvotes: 0

Views: 177

Answers (2)

Carsten
Carsten

Reputation: 943

If you want to transform your array elements into a new array, you could also use Array.map.

allUsers = users.map(user => user.data());

Upvotes: 1

H&#233;ctor
H&#233;ctor

Reputation: 26044

You can't index an array using a string. To solve it, you can:

Use forEach index as array index:

users.forEach((user, i) => {
    allUsers[i] = user.data();
});

or push items to the allUsers array:

users.forEach(user => {
    allUsers.push(user.data());
});

Upvotes: 0

Related Questions