node.js - Firestore for Node.js 最终会转向 Web 中使用的新模块化 v9 吗?

标签 node.js firebase google-cloud-firestore

抱歉,如果问这个问题的地方不对,但不确定这个问题会去哪里!我正在使用 Firebase Firestore 和 Functions 构建一个应用程序,并且希望偶尔在前端和后端之间使用相同的查询片段,但现在 v9 语法与 v8 语法有很大不同。我们最终会在 Node.js 端看到相同的 v9 语法吗?

更新:

更新到 Node 16 后,模块化语法仍然无法工作。也许我的代码片段是错误的? TSLint 强调在 updateDoc() 中使用 db 时出现错误:

No overload matches this call.
  Overload 1 of 3, '(firestore: Firestore, path: string, ...pathSegments: string[]): DocumentReference<DocumentData>', gave the following error.
    Argument of type 'FirebaseFirestore.Firestore' is not assignable to parameter of type 'import("C:/Users/dougl/Documents/GitHub/fire-react-base/functions/node_modules/@firebase/firestore/dist/index").Firestore'.
      Type 'Firestore' is missing the following properties from type 'Firestore': type, app, toJSON
  Overload 2 of 3, '(reference: CollectionReference<unknown>, path?: string | undefined, ...pathSegments: string[]): DocumentReference<unknown>', gave the following error.
    Argument of type 'Firestore' is not assignable to parameter of type 'CollectionReference<unknown>'.
      Type 'Firestore' is missing the following properties from type 'CollectionReference<unknown>': type, id, path, parent, and 3 more.
  Overload 3 of 3, '(reference: DocumentReference<unknown>, path: string, ...pathSegments: string[]): DocumentReference<DocumentData>', gave the following error.
    Argument of type 'Firestore' is not assignable to parameter of type 'DocumentReference<unknown>'.
      Type 'Firestore' is missing the following properties from type 'DocumentReference<unknown>': converter, type, firestore, id, and 3 more.ts(2769)

Firebase 功能:

"use strict";
import functions = require("firebase-functions");
import {DocumentSnapshot, getFirestore} from "firebase-admin/firestore";
import { doc, increment, updateDoc } from "firebase/firestore"; 
import { initializeApp } from "firebase-admin";

const fire = initializeApp(functions.config().firebase);
const db = getFirestore(fire);

export const onMessageCreated = functions.firestore
    .document("messages/{messageId}")
    .onCreate(async (snap: DocumentSnapshot, context: functions.EventContext) => {
        const newValues = snap.data();
        if (!newValues) {
            return;
        } else {
            console.log("newValues: ");
            console.log(newValues);
        }

        try {
            const allPromises: Array<Promise<any>> = [];

            await updateDoc(doc(db, "site", "counts"), {
                messages: increment(1)
            }).then(() => {
                console.log(`Incremented messageCount`)
            }).catch(error => {
                console.error(`Error incrementing messageCount: ${error}`)
            })

            return Promise.all(allPromises);
        } catch (error) {
            console.error(error);
            return;
        }
});

函数package.json:

{
  "name": "functions",
  "scripts": {
    "lint": "eslint --ext .js,.ts .",
    "build": "tsc",
    "serve": "npm run build && firebase emulators:start --only functions",
    "shell": "npm run build && firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "16"
  },
  "main": "lib/index.js",
  "dependencies": {
    "firebase": "^9.6.8",
    "firebase-admin": "^10.0.2",
    "firebase-functions": "^3.19.0"
  },
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^3.9.1",
    "@typescript-eslint/parser": "^3.8.0",
    "eslint": "^7.6.0",
    "eslint-config-google": "^0.14.0",
    "eslint-plugin-import": "^2.22.0",
    "firebase-functions-test": "^0.2.0",
    "typescript": "^4.6.2"
  },
  "private": true
}

最佳答案

从版本 10 开始,Node.js 管理 SDK 支持与客户端 JavaScript SDK 版本 9 及更高版本相同的模块化语法。要开始使用,请查看 Upgrade to Node.js SDK Admin SDK v10 (modular SDK) 上的文档。 .

关于node.js - Firestore for Node.js 最终会转向 Web 中使用的新模块化 v9 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71418793/

相关文章:

ios - 在另一个选项卡中加载相同的 View Controller 时,文档快照监听器不起作用! - swift

firebase - 按时间戳月份查询 Firebase 数据

node.js - 云函数崩溃并显示 `Error: Can' 无法确定 Firebase 数据库 URL。

javascript - 用于循环异步等待的 Firebase 函数

ios - 使用 GeoFire 检查附近是否没有位置

javascript - Firestore - 查询并动态设置 Refs 函数

node.js - Electron/NodeJS : I can't retriving mysql table data and print them on the index. html 页面

javascript - 在 Express.js 应用程序中 require() 某物有多昂贵?

node.js - Mongo 在内部和对象数组中查找多个匹配项

ios - 如何在 firebase 的实时数据库中发送新消息的推送通知