【问题标题】:Firebase cloud Parsing error: Unexpected token adminFirebase 云解析错误:意外的令牌管理员
【发布时间】:2020-09-14 12:13:38
【问题描述】:

在创建“points”类时,我正在寻找用户并用 100(num 值)更新它的点,但我不断得到那个 referuserRef 出乎意料 这是我的代码

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
}

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore google-cloud-functions


    【解决方案1】:

    根据您的评论更新

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

    所以,以下应该可以解决问题:

    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 字段的值。


    旧答案:

    您似乎没有加载 Cloud Functions SDK。你应该这样做:

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

    【讨论】:

    • 可以分享functions目录下的package.json文件吗?
    • 嗯...我看不出问题出在哪里...我将在 10 分钟内删除我的答案。但是请注意,您使用 await 时没有声明函数 async,而且,您没有正确管理 yoru Cloud Function 的生命周期。请参阅firebase.google.com/docs/functions/terminate-functions,因为它非常重要。
    • 如果我删除 await,它会部署但不执行.... api 请求上的“错误:无法处理请求”
    • 它直接用“TypeError: snap.data is not a function”捕获错误
    • 我建议您从一个全新的 Firebase 项目开始。我刚刚测试了我的答案中的代码,它确实有效,正确地增加了现有用户文档中的字段。
    猜你喜欢
    • 2021-07-08
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2022-01-02
    • 2019-02-19
    • 2017-11-21
    • 2013-09-26
    • 2018-09-12
    相关资源
    最近更新 更多