3

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
}
pb4now
  • 1,305
  • 4
  • 18
  • 45

1 Answers1

5

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(...)
Titian Cernicova-Dragomir
  • 230,986
  • 31
  • 415
  • 357
  • ty for that but error "Property 'map' does not exist on type 'QuerySnapshot'" is still there :s – pb4now Apr 25 '18 at 10:13
  • @pb4now Sorry about that, it seemed like a simple problem and I must admit I did not run the code. You have another issue in the code, namely the returned values are in the `docs` property – Titian Cernicova-Dragomir Apr 25 '18 at 10:24
  • 1
    great!!! that solved it! 3h of reainstalating every single thing and trying to figure it out. ty once again (y) – pb4now Apr 25 '18 at 10:30