firebase - 谷歌云功能:警告,FIREBASE_CONFIG 和 GCLOUD_PROJECT 环境变量丢失。初始化 firebase-admin 将失败
问题描述
我有一个谷歌云功能,当文档添加到 Cloud Firestore 时,它会向我发送一封电子邮件。
- 运行时环境是:Nodejs 14
该功能确实有效,但在日志中我收到警告“警告,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
}
解决方案
我通过使用 firebase cli 部署功能解决了这个问题。这样变量就会自动填充。
查看此链接以获取示例:Firebase CLI 参考
推荐阅读
- vb.net - 将数据从 Ms-access 传递到水晶报告到详细信息部分 vb.net
- python - 将列表拆分为较小的大小相等的列表,保持“中间值”?
- spring-boot - 是否可以在 Spring Data Rest 中通过关联进行搜索?
- reactjs - 托管在 AWS S3 API 请求上的 React 应用程序失败
- c# - 如何在asp.net的gridview中标记标记为“已确认”的模板字段?
- python-3.x - 使用 Python3 进行网页抓取
- javascript - 如何删除一个在 JS 中使用按钮
- python - 使用 python 将具有不同架构的项目附加到 Avro 中的现有文件
- python - 如何在日志文件中记录串口输入?
- c++ - 了解 C 中的线程以及如何使用它们进行排序