pb4now
pb4now

Reputation: 1295

Node, firebase functions Property 'map' does not exist on type 'Promise<QuerySnapshot>

Firstly that does not helps me: property map does not exist and importing 'rxjs' does nork either

just testing using typescript with my functions and using "async" and "await" keywords for some reason I'm getting error when trying to use map here is my code(btw if the way I retriving data from firestore using "await" is incorrect I would be very greatfull if someone correct me) -could not find alot of examples that uses typescript/functions/firestore):

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

admin.initializeApp(functions.config().firebase);
let db = admin.firestore();

export const helloWorld = functions.https.onRequest(
  async (request, response) => {
    try {
      let t = await getEmployees()
      response.send("yes");
    } catch (e) {
      response.send('no')
    }
});


async function getEmployees(): Promise<any> {
  try {
    const f = await db.collection('companies').get().map(value => {
        let id = value.id
        return id;
    })
   } catch (e) {
    return e;
   }
}

typescript Version 2.8.3

package.json:

{
"name": "functions",
"scripts": {
"lint": "tslint --project tsconfig.json",
"build": "tsc",
"serve": "npm run build && firebase serve --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^5.12.0",
"firebase-functions": "^1.0.1"
},
"devDependencies": {
"tslint": "^5.8.0",
"typescript": "2.8.3"
  },
"private": true
}

Upvotes: 3

Views: 2141

Answers (1)

Titian Cernicova-Dragomir
Titian Cernicova-Dragomir

Reputation: 249556

await will be applied over the result of map, you are essentially calling :

const f = await (db.collection('companies').get().map(...))

You want to call map on the result of awaiting get, and you should add parenthesis to tell the compiler this is what you want tot do. Also you are probably looking for the docs property on the snapshot (which returns the results of the query)

const f = (await db.collection('companies').get()).docs.map(...)

Upvotes: 5

Related Questions