![](/img/trans.png)
[英]Nodejs/Mocha - FieldValue.increment - FirebaseError: Function DocumentReference.update() called with invalid data
[英]Google cloud functions Firebase Update FieldValue.increment(1) using NodeJS - TypeError: FieldValue.increment is not a function
我在谷歌雲功能中使用 NodeJs,我想增加一個文檔中一個參數的值
我啟動 Firestore:
const Firestore = require('@google-cloud/firestore');
const PROJECTID = 'XXXX';
const firestore = new Firestore({
projectId: PROJECTID,
timestampsInSnapshots: true
});
並且 my.get() 和 .set() 函數可以正常工作。 但是當我嘗試更新一個增加它的值時,我收到 FieldValue.increment 錯誤
我試過了:
await snapshot.ref.update({ parameter: FieldValue.increment(1) });
添加:
const FieldValue = require('firebase-admin').firestore.FieldValue;
await snapshot.ref.update({ parameter: FieldValue.increment(1) });
如其他網站所述,並且:
documentRef.update(
'parameter', Firestore.FieldValue.increment(1)
)
如https://cloud.google.com/nodejs/docs/reference/firestore/latest/firestore/fieldvalue中所述
const admin = require('firebase-admin');
increment = admin.firestore.FieldValue.increment(1);
await snapshotRef.update({ parameter: increment });
但沒有一個工作。
錯誤:
類型錯誤:admin.firestore.FieldValue.increment 不是 function
或者
類型錯誤:FieldValue.increment 不是 function
"dependencies": {
"firebase-admin": "^6.5.1",
"nodemailer": "^6.6.1"
}
如發行說明中所述, increment()
已於 2019 年 4 月添加到 Firestore SDK,但 Firebase Admin 6.5.1
於 2019 年 1 月發布。升級到最新版本應該可以解決此問題。
只需添加命令即可更新到最新的 Firebase Admin 版本。
npm i firebase-admin@latest
您還可以通過將版本更改為最新版本來修改 package.json 中的特定package.json
。 請參閱下面的示例:
"firebase-admin": "latest"
然后運行
npm install
此外,為獲得最佳實踐,請始終刪除node_modules
文件夾和package-lock.json
,然后在部署應用程序之前運行npm install
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.