首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >谷歌云功能:警告,FIREBASE_CONFIG和GCLOUD_PROJECT环境变量丢失。初始化防火墙-管理员将失败

谷歌云功能:警告,FIREBASE_CONFIG和GCLOUD_PROJECT环境变量丢失。初始化防火墙-管理员将失败
EN

Stack Overflow用户
提问于 2021-09-28 13:56:28
回答 1查看 1.5K关注 0票数 0

我有一个google云功能,当文档被添加到云消防局时,它会给我发送一封电子邮件。

  • 运行时环境是: Nodejs 14

这个函数确实有效,但是在日志中我得到了一个警告:“警告,FIREBASE_CONFIG和GCLOUD_PROJECT环境变量丢失了。初始化firebase-admin会失败”--我真的不明白这个警告。

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

我看到一些人使用NodeJS8,它起了作用,但我不想使用NodeJS8。

我对云很陌生。我说的是几个星期。请不要让你回答复杂。

以下是代码:

代码语言:javascript
运行
复制
"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

代码语言:javascript
运行
复制
{
  "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
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-10-06 07:32:14

我通过使用firebase部署函数来解决这个问题。这样,变量就会自动填充。

查看此链接以获得一个示例:火基CLI参考

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69363102

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档