use*_*961 7 firebase typescript firebase-tools google-cloud-functions google-cloud-firestore
我正在 firebase 模拟器中测试云功能,并在尝试增加云 firestore 中的字段时收到错误。请检查下面我的代码和错误消息。谢谢你的帮助!
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
admin.initializeApp();
const db = admin.firestore();
export const newUserIncrementStat = functions.firestore.document("users/{uid}").onCreate((snap, context) => {
const docRef = db.doc("stats/users");
try {
return docRef.set({
totalUsers: admin.firestore.FieldValue.increment(1),
}, {merge: true});
} catch (e) {
console.log("Something is wrong: ", e);
return Promise.reject(e);
}
});
Run Code Online (Sandbox Code Playgroud)
Firebase 日志中的错误消息:
类型错误:无法读取未定义的属性(读取“增量”)
依赖关系
firebase 管理员:“^11.0.0”
firebase 函数:“^3.22.0”
火力基地:11.3.0
小智 9
这是 firebase-admin 模块化改进的问题。您可以回滚到 firebase-admin v10.3.0 或使用模块化导入import { FieldValue } from 'firebase-admin/firestore';
import * as functions from "firebase-functions";
import * as admin from "firebase-admin";
import { FieldValue } from 'firebase-admin/firestore';
admin.initializeApp();
const db = admin.firestore();
export const newUserIncrementStat =
functions.firestore.document("users/{uid}").onCreate((snap, context) =>
{
const docRef = db.doc("stats/users");
try {
return docRef.set({
totalUsers: FieldValue.increment(1),
}, {merge: true});
} catch (e) {
console.log("Something is wrong: ", e);
return Promise.reject(e);
}
});
Run Code Online (Sandbox Code Playgroud)
或者您可以完全转换为模块化导入:
import type { FieldValue, getFirestore } from 'firebase-admin/firestore';
import * as functions from "firebase-functions";
const { initializeApp } = require('firebase-admin/app');
const app = initializeApp();
const db = getFirestore();
export const newUserIncrementStat =
functions.firestore.document("users/{uid}").onCreate((snap, context) =>
{
const docRef = db.doc("stats/users");
try {
return docRef.set({
totalUsers: FieldValue.increment(1),
}, {merge: true});
} catch (e) {
console.log("Something is wrong: ", e);
return Promise.reject(e);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1722 次 |
| 最近记录: |