首页 > 解决方案 > 谷歌云功能:警告,FIREBASE_CONFIG 和 GCLOUD_PROJECT 环境变量丢失。初始化 firebase-admin 将失败

问题描述

我有一个谷歌云功能,当文档添加到 Cloud Firestore 时,它​​会向我发送一封电子邮件。

该功能确实有效,但在日志中我收到警告“警告,FIREBASE_CONFIG 和 GCLOUD_PROJECT 环境变量丢失。初始化 firebase-admin 将失败”我不太明白这个警告。

你知道有什么解决办法吗?我不想看到这个警告。我该怎么办(解决方案)。

我看到有些人使用 nodejs 8 并且它有效,但我不想使用 nodejs 8。

我对云的东西很陌生。我说的是几周。请不要让你回答复杂。

这是代码:

"use strict";

const functions = require("firebase-functions");
const admin = require("firebase-admin");
const nodemailer = require("nodemailer");

const FireStoreParser = require ("firestore-parser");

const { google } = require("googleapis");

admin.initializeApp();

exports.sendMail = functions.handler.firestore.document.onCreate(async(change,context) => {
  
  const OAuth2 = google.auth.OAuth2;
  const clientID = "you-dont-need-this";
  const clientSecret = "you-dont-need-this";
  const refreshToken = "you-dont-need-this"
  
  const oauth2Client = new OAuth2(
    clientID, //client Id
    clientSecret, // Client Secret
    "https://developers.google.com/oauthplayground" // Redirect URL
  );

  oauth2Client.setCredentials({
    refresh_token: refreshToken
  });
  
  const accessToken = await oauth2Client.getAccessToken()
    
  const smtpTransport = nodemailer.createTransport({
    service: "gmail",
    auth: {
      type: "OAuth2",
      user: "you-dont-need-this",
      clientId: clientID,
      clientSecret: clientSecret,
      refreshToken: refreshToken,
      accessToken: accessToken
    }
  });
  
  const _fieldsProtoInJSON = FireStoreParser(change._fieldsProto);
  const textToMail = JSON.stringify(_fieldsProtoInJSON);
  
  var attachment = [
    {   // binary buffer as an attachment
        filename: 'dataContainer.json',
        content: textToMail
    }
  ];
  
  const mailOptions = {
    from: `<you-dont-need-this>`,
    to: 'you-dont-need-this,
    subject: `New message container ${_fieldsProtoInJSON.ContainerNumber} from ${_fieldsProtoInJSON.Tag}`,
    text: `See attachment for data.`,
    attachments: attachment
  };
  

  smtpTransport.sendMail(mailOptions, (error, info) => {
    if (error) {
      console.log(error.message);
      smtpTransport.close();
    }
    return "mail sent";
  });
});

这是 package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "14"
  },
  "dependencies": {
    "firebase-admin": "^8.12.1",
    "firebase-functions": "^3.6.2",
    "firestore-parser": "0.9.0",
    "@google-cloud/storage": "^5.1.1",
    "googleapis": "^51.0.0",
    "nodemailer": "^6.4.8"
  },
  "devDependencies": {
    "eslint": "^5.12.0",
    "eslint-plugin-promise": "^4.0.1"
  },
  "private": true
}

标签: firebasegoogle-cloud-firestoregoogle-cloud-functions

解决方案


我通过使用 firebase cli 部署功能解决了这个问题。这样变量就会自动填充。

查看此链接以获取示例:Firebase CLI 参考


推荐阅读