我有一个google云功能,当文档被添加到云消防局时,它会给我发送一封电子邮件。
这个函数确实有效,但是在日志中我得到了一个警告:“警告,FIREBASE_CONFIG和GCLOUD_PROJECT环境变量丢失了。初始化firebase-admin会失败”--我真的不明白这个警告。
你知道有什么解决办法吗?我不想看到这个警告。我必须做什么(解决方案)。
我看到一些人使用NodeJS8,它起了作用,但我不想使用NodeJS8。
我对云很陌生。我说的是几个星期。请不要让你回答复杂。
以下是代码:
"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
}
发布于 2021-10-05 23:32:14
我通过使用firebase部署函数来解决这个问题。这样,变量就会自动填充。
查看此链接以获得一个示例:火基CLI参考
https://stackoverflow.com/questions/69363102
复制相似问题