【发布时间】:2021-09-28 13:56:28
【问题描述】:
我有一个谷歌云功能,当文档添加到 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
}
【问题讨论】:
-
你搜索了吗?这是one result
-
是的,我看到他们说设置变量 GCLOUD_PROJECT = project.id。我不知道在哪里做这个。我没有使用任何 SDK,我在侧谷歌云平台中执行此操作。你知道该放在哪里吗?
-
我也看到了那个。我在节点 14 上。那个错误还在吗?
标签: firebase google-cloud-firestore google-cloud-functions