node.js - Node ,firebase 函数属性 'map' 在类型“Promise<QuerySnapshot>”上不存在

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

首先,这对我没有帮助:property map does not exist并且导入 'rxjs' 也不会 Nork

只是使用 typescript 与我的函数进行测试,并使用“async”和“await”关键字,出于某种原因,我在尝试使用 map 时遇到错误,这是我的代码(顺便说一句,如果我使用“await”从 firestore 检索数据的方式不正确,如果有人纠正我,我将非常感激)-找不到很多使用 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 版本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
}

最佳答案

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

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

您想要在等待 get 的结果上调用 map,并且您应该添加括号来告诉编译器这就是您想要执行的操作。此外,您可能正在寻找快照上的 docs 属性(它返回查询结果)

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

关于node.js - Node ,firebase 函数属性 'map' 在类型“Promise<QuerySnapshot>”上不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50019304/

相关文章:

mysql - Passport Node (错误 : Failed to deserialize user out of session.)

visual-studio - ts 属性推送在 Angular 代码 VS 中的类型 '{}' 上不存在

javascript - 无法从服务订阅数据到组件

javascript - TypeScript UMD 'module' 和 'define' 都未定义

javascript - Node.js出现 "Cannot set headers after they are sent to the client"错误怎么解决?

node.js - 如何使用 Nodejs 在 post 请求之间传输数据

javascript - 跨模块共享事件

javascript - Express Server 绕过 CORS

firebase - 谷歌Firestore : Query on substring of a property value (text search)

firebase - 检测到零个或 2 个或更多 [DropdownMenuItem] 具有相同的值