首页 > 解决方案 > 节点,firebase 函数属性“地图”在“承诺”类型上不存在

问题描述

首先,这对我没有帮助:属性映射不存在并且导入“rxjs”也不起作用

只是在我的函数中使用 typescript 并使用“async”和“await”关键字由于某种原因我在尝试使用 map 时遇到错误这是我的代码(顺便说一句,如果我使用“await”从 firestore 检索数据的方式不正确如果有人纠正我,我会非常高兴)-找不到很多使用打字稿/函数/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;
   }
}

打字稿版本 2.8.3

包.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
}

标签: node.jstypescriptgoogle-cloud-firestoregoogle-cloud-functions

解决方案


await将应用于 的结果map,您实际上是在调用:

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

你想调用mapawaiting 的结果get,你应该添加括号来告诉编译器这是你想要做的。此外,您可能正在寻找docs快照上的属性(返回查询结果)

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

推荐阅读