Firebase云分析错误:意外的令牌管理



依此类推"点";创建了类,我找到了用户并用100(num值(更新了它的点数,但我一直得到了意外的referruserRef这是我的代码

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.pointsCreated = functions.firestore
.document('points/{pointsid}')
.onCreate((snap, context) => {

const newValue = snap.data();
if (newValue['type'] !== null) {
if (newValue['type'] === 'refer') {
const referuserRef = admin.firestore().collection('users').doc(newValue['user']); //this is where i get the error
console.log("referuserRef");
console.log(referuserRef);
const referuserDoc = await referuserRef.get()
if (referuserDoc.exists) {
referuserRef.set({ points: Number(referuserDoc.data()['points']) + 100 }, { merge: true });
}
}
}
});

这是package.json和.eslintrc.json的"ecmaVersion":7(我也试过8次,但每次尝试都会出现同样的错误

{
"name": "functions",
"description": "Cloud Functions for Firebase",
"scripts": {
"lint": "eslint .",
"serve": "firebase emulators:start --only functions",
"shell": "firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "10"
},
"main": "index.js",
"dependencies": {
"firebase-admin": "^8.10.0",
"firebase-functions": "^3.6.1"
},
"devDependencies": {
"eslint": "^5.12.0",
"eslint-plugin-promise": "^4.0.1",
"firebase-functions-test": "^0.2.0"
},
"private": true
}

根据您的评论进行更新

您需要正确管理云函数的生命周期,等待异步Firebase方法调用完成,然后向云函数平台指示它可以清理您的函数。有关更多详细信息,请参阅此文档。

因此,以下应该可以做到这一点:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const FieldValue = require('firebase-admin').firestore.FieldValue;
admin.initializeApp();
exports.pointsCreated = functions.firestore
.document('points/{pointsid}')
.onCreate(async (snap, context) => {
try {
const newValue = snap.data();
if (newValue['type'] !== null) {
if (newValue['type'] === 'refer') {
const referuserRef = admin.firestore().collection('users').doc(newValue['user']);
const referuserDoc = await referuserRef.get();
if (referuserDoc.exists) {
await referuserRef.set({ points: FieldValue.increment(100) }, { merge: true });
return null;
} else {
return null;
}
} else {
return null;
}
} else {
return null;
}
} catch (error) {
// ...
return null;
}
});

注意使用FieldValue来增加points字段的值。


旧答案:

您似乎没有加载云函数SDK。你应该做如下:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
// ...

相关内容

  • 没有找到相关文章

最新更新