我已经尝试部署云函数一个多星期了,但是我总是在日志中出现错误并且函数没有部署。
我在日志中收到以下错误:
{"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":3,"message":"Build failed: npm ERR! cipm can only install packages when your package.json and package-lock.json or npm-shrinkwrap.json are in sync. Please update your lock file with `npm install` before continuing.\nnpm ERR! \nnpm ERR! \nnpm ERR! Missing: nodemailer@^6.6.0\nnpm ERR! \n\nnpm ERR! A complete log of this run can be found in:\nnpm ERR! /builder/home/.npm/_logs/2021-05-13T16_38_49_151Z-debug.log; Error ID: beaf8772"}
这是我的函数代码:
const functions = require("firebase-functions");
const nodemailer = require("nodemailer");
const admin = require("firebase-admin");
const serviceAccount = require("admin_sdk.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://enjoylearning-a3ce3.firebaseio.com",
});
admin.firestore().settings({timestampsInSnapshots: true});
const db = admin.firestore();
// For Gmail, enable these:
// 1. https://www.google.com/settings/security/lesssecureapps
// 2. https://accounts.google.com/DisplayUnlockCaptcha
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
// auth.languageCode = 'ar';
const gmailEmail = XXXX;
const gmailPassword = XXXX;
const mailTransport = nodemailer.createTransport({
service: "gmail",
auth: {
user: gmailEmail,
pass: gmailPassword,
},
});
// Your company name to include in the emails
// TODO: Change this to your app or company name to customize the email sent.
const APP_NAME = "EduCup";
// [START sendWelcomeEmail]
/**
* Sends a welcome email to new user.
*/
// [START onCreateTrigger]
/* exports.sendWelcomeEmail = functions.auth.user().onCreate((user) => {
// [END onCreateTrigger]
// [START eventAttributes]
const email = user.email; // The email of the user.
const displayName = user.displayName; // The display name of the user.
return sendWelcomeEmail(email, displayName);
}); */
exports.sendWelcomeEmail = functions.firestore
.document("users/{user}")
.onCreate(async (snap, context) => {
const email = snap.data().userEmail; // The email of the user.
const displayName = snap.data().userName; // The display name of the user.
const uid = snap.data().userId;
return sendWelcomeEmail(email, displayName, uid);
});
// [END sendWelcomeEmail]
// [START onCreateTrigger]
exports.addPoints = functions.firestore
.document("users/{user}")
.onCreate(async (snap, context) => {
const invitingFriendId = snap.data().invitingFriendId;
const newUserEmail = snap.data().userEmail;
const invitingFriendRef = db.collection("users").doc(invitingFriendId);
let friendEmail;
const transaction = db.runTransaction((t) => {
return t.get(invitingFriendRef).then((doc) => {
// Add to the chain count
const userData = doc.data();
const newPoints = userData.points + 50;
// let newCompetitionPoints = userData.competitionPoints + 50;
let newInviteesNo = userData.inviteesNo + 1;
// console.log("competition points is : " + newCompetitionPoints);
/* if(isNaN(newCompetitionPoints)||
newCompetitionPoints == undefined ||
newCompetitionPoints == null){
newCompetitionPoints = Number(50);
console.log("if called , newCompetition points is : " +
newCompetitionPoints);
} */
if (isNaN(newInviteesNo)|| newInviteesNo == undefined ||
newInviteesNo == null) {
newInviteesNo = Number(1);
console.log("if called , newInviteesNo is : " + newInviteesNo);
}
friendEmail = doc.data().userEmail;
t.update(invitingFriendRef, {inviteesNo: newInviteesNo,
points: newPoints});
console.log("friend email is : " + friendEmail +
" , new userEmail is : " + newUserEmail);
});
}).then((result) => {
console.log("Transaction success!");
return sendNewFriendAddedEmail(friendEmail, newUserEmail);
}).catch((err) => {
console.log("Transaction failure:", err);
return true;
});
});
/*
exports.addSecondChallengerPoints = functions.firestore.document
(`challenges/{challengeId}`).onUpdate(async (snap, context) => {
const player1Score = snap.after.data().player1score;
const player1Uid = snap.after.data().player1Uid;
const player2Score = snap.after.data().player2score;
const challengeId = snap.after.data().challengeId;
const score1Added = snap.after.data().score1Added;
const drawChallengePoints = 1;
const winChallengepoints = 5;
const opponentRef = db.collection('users').doc(player1Uid);
const challengeRef = db.collection('challenges').doc(challengeId);
let transaction = db.runTransaction(t => {
return t.get(opponentRef).then(doc => {
const noOfWins = doc.data().noOfWins;
const noOfDraws = doc.data().noOfDraws;
const noOfLoses = doc.data().noOfLoses;
const totalChallengesNo = doc.data().totalChallengesNo;
const oldPoints = doc.data().points;
console.log("player1Score : " + player1Score + " , player2Score : "
+ player2Score
+ " old points : " + oldPoints + " , totalChallengesNo : "
+ totalChallengesNo);
if(player1Score == player2Score){
const newPoints = oldPoints + drawChallengePoints;
t.update(challengeRef, {"score1Added": true});
t.update(opponentRef, {"points": newPoints});
t.update(opponentRef,
{"totalChallengesNo": totalChallengesNo + 1});
t.update(opponentRef, {"noOfDraws": noOfDraws + 1});
} else if(player1Score > player2Score){
const newPoints = oldPoints + winChallengepoints;
t.update(challengeRef, {"score1Added": true});
t.update(opponentRef, {"points": newPoints});
t.update(opponentRef, {"totalChallengesNo":
totalChallengesNo + 1});
t.update(opponentRef, {"noOfWins": noOfWins + 1});
} else {
t.update(challengeRef, {"score1Added": true});
t.update(opponentRef, {"totalChallengesNo":
totalChallengesNo + 1});
t.update(opponentRef, {"noOfLoses": noOfLoses + 1});
}
});
}).then(result => {
console.log('Transaction success!');
}).catch(err => {
console.log('Transaction failure:', err);
});
return true;
});
*/
// [START sendByeEmail]
/**
* Send an account deleted email confirmation to users
* who delete their accounts.
*/
/* TODO :remove this comment to add goodbye email
// [START onDeleteTrigger]
exports.sendByeEmail = functions.auth.user()
.onDelete((user) => {
// [END onDeleteTrigger]
const email = user.email;
const displayName = user.displayName;
return sendGoodbyeEmail(email, displayName);
});
// [END sendByeEmail]
*/
// Sends a welcome email to the given user.
function sendWelcomeEmail(email, displayName, uid) {
const mailOptions = {
from: `${APP_NAME} <noreply@firebase.com>`,
to: email,
};
// The user subscribed to the newsletter.
mailOptions.subject = "welcome to our app! ";
const strtText = "Welcome to our app! We hope you enjoy it.";
const pageLink = "To know the latest news about the app and the latest competitions please join our facebook group : \n\n https://www.facebook.com/groups/434661403874384/";
mailOptions.text = strtText + "\n\n" + pageLink;
// TODO : add new line instead of space
return mailTransport.sendMail(mailOptions).then(() => {
return console.log("New welcome email sent to:", email);
});
}
// Sends a welcome email to the given user.
function sendNewFriendAddedEmail(friendEmail, newUserEmail) {
const mailOptions = {
from: `${APP_NAME} <noreply@firebase.com>`,
to: friendEmail,
};
console.log("sendNewFriendAddedEmail called");
// The user subscribed to the newsletter.
mailOptions.subject = "You won 50 points!";
const strtText="We are glad to inform you that your friend whose email is \""+
newUserEmail +"\" has registered to the app using your code and you"+
" have got 50 points in the app."+
" \n\nInvite more friends to get more points in our app.";
mailOptions.text = strtText;
/* return mailTransport.sendMail(mailOptions).then(() => {
return console.log('New points email sent to:', friendEmail);
}) */
return mailTransport.sendMail(mailOptions, function(error, info) {
if (error) {
return console.log(error);
}
else {
return console.log("Email sent: " + info.response);
}
});
}
我尝试了不同的解决方案,但仍然出现上述错误,我仍然无法部署我的功能