节点,firebase功能属性&#39; map&#39;类型&#39; Promise <querysnapshot>上不存在

时间:2018-04-25 09:58:54

标签: node.js typescript google-cloud-firestore google-cloud-functions

首先,这对我没有帮助:property map does not exist并导入&#39; rxjs&#39;做nork

只需使用我的函数使用typescript并使用&#34; async&#34;和#34;等待&#34;关键字由于某种原因我在尝试使用map时遇到错误是我的代码(顺便说一句,如果我使用&#34从firestore中恢复数据的方式;等待&#34;不正确我会非常非常好,如果有人纠正我的话) - 无法找到很多使用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;
   }
}

打字稿版本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
}

1 个答案:

答案 0 :(得分:1)

await将应用于map的结果,您基本上是在致电:

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

您希望在等待map的结果上调用get,并且您应该添加括号以告诉编译器这是您想要做的事情。您也可能在快照上查找docs属性(返回查询结果)

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