Javascript 部署新更改后不显示Firebase功能

Javascript 部署新更改后不显示Firebase功能,javascript,firebase,google-cloud-functions,Javascript,Firebase,Google Cloud Functions,我从示例函数中复制了一个电子邮件确认云函数,并将其部署到我的项目中,它可以工作,并且该函数显示在我的firebase控制台中 但是,在我根据需要定制代码并部署到firebase之后,该函数就从我的firebase函数中消失了 原代码为: 'use strict'; const functions = require('firebase-functions'); const nodemailer = require('nodemailer'); // Configure the email tr

我从示例函数中复制了一个电子邮件确认云函数,并将其部署到我的项目中,它可以工作,并且该函数显示在我的firebase控制台中

但是,在我根据需要定制代码并部署到firebase之后,该函数就从我的firebase函数中消失了

原代码为:

'use strict';

const functions = require('firebase-functions');
const nodemailer = require('nodemailer');
// Configure the email transport using the default SMTP transport and a GMail account.
// For other types of transports such as Sendgrid see https://nodemailer.com/transports/
// TODO: Configure the `gmail.email` and `gmail.password` Google Cloud environment variables.
const gmailEmail = functions.config().gmail.email;
const gmailPassword = functions.config().gmail.password;
const mailTransport = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: gmailEmail,
    pass: gmailPassword,
  },
});

// Sends an email confirmation when a user changes his mailing list subscription.
exports.sendEmailConfirmation = functions.database.ref('/users/{uid}').onWrite((event) => {
  const snapshot = event.data;
  const val = snapshot.val();

  if (!snapshot.changed('subscribedToMailingList')) {
    return null;
  }

  const mailOptions = {
    from: '"Spammy Corp." <noreply@firebase.com>',
    to: val.email,
  };

  const subscribed = val.subscribedToMailingList;

  // Building Email message.
  mailOptions.subject = subscribed ? 'Thanks and Welcome!' : 'Sad to see you go :`(';
  mailOptions.text = subscribed ? 'Thanks you for subscribing to our newsletter. You will receive our next weekly newsletter.' : 'I hereby confirm that I will stop sending you the newsletter.';

  return mailTransport.sendMail(mailOptions)
    .then(() => console.log(`New ${subscribed ? '' : 'un'}subscription confirmation email sent to:`, val.email))
    .catch((error) => console.error('There was an error while sending the email:', error));
});
以下是Cmder对新代码部署的响应:

=== Deploying to 'food-ninja-mobile'...               

i  deploying database, functions, hosting             
Running command: npm --prefix "$RESOURCE_DIR" run lint


> functions@ lint C:\Users\jerry.ho\Documents\ADA\Proj
ects\foodNinjaCloudFunction\email-confirmation\functio
ns                                                    
> eslint .                                            

+  functions: Finished running predeploy script.      
i  database: checking rules syntax...                 
+  database: rules syntax for database food-ninja-mobi
le is valid                                           
i  functions: ensuring necessary APIs are enabled...  
+  functions: all necessary APIs are enabled          
i  functions: preparing functions directory for upload
ing...                                                
i  hosting: preparing public directory for upload...  
+  hosting: 4 files uploaded successfully             
i  database: releasing rules...                       
+  database: rules for database food-ninja-mobile rele
ased successfully                                     
i  functions: deleting function sendEmailConfirmation.
..                                                    
+  functions[sendEmailConfirmation]: Successful delete
 operation.                                           

+  Deploy complete!                                   

Project Console: https://console.firebase.google.com/p
roject/food-ninja-mobile/overview                     
Hosting URL: https://food-ninja-mobile.firebaseapp.com
=== Deploying to 'food-ninja-mobile'...

i  deploying database, functions, hosting
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint C:\...\---local path---
> eslint .

+  functions: Finished running predeploy script.
i  database: checking rules syntax...
+  database: rules syntax for database food-ninja-mobile is valid
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (52.54 KB) for uploading
+  functions: functions folder uploaded successfully
i  hosting: preparing public directory for upload...
+  hosting: 4 files uploaded successfully
i  database: releasing rules...
+  database: rules for database food-ninja-mobile released successfully
i  functions: creating function sendEmailConfirmation...
+  functions[sendEmailConfirmation]: Successful create operation.

+  Deploy complete!

Project Console: https://console.firebase.google.com/project/food-ninja-mobile/overview
Hosting URL: https://food-ninja-mobile.firebaseapp.com
以下是原始代码部署的响应:

=== Deploying to 'food-ninja-mobile'...               

i  deploying database, functions, hosting             
Running command: npm --prefix "$RESOURCE_DIR" run lint


> functions@ lint C:\Users\jerry.ho\Documents\ADA\Proj
ects\foodNinjaCloudFunction\email-confirmation\functio
ns                                                    
> eslint .                                            

+  functions: Finished running predeploy script.      
i  database: checking rules syntax...                 
+  database: rules syntax for database food-ninja-mobi
le is valid                                           
i  functions: ensuring necessary APIs are enabled...  
+  functions: all necessary APIs are enabled          
i  functions: preparing functions directory for upload
ing...                                                
i  hosting: preparing public directory for upload...  
+  hosting: 4 files uploaded successfully             
i  database: releasing rules...                       
+  database: rules for database food-ninja-mobile rele
ased successfully                                     
i  functions: deleting function sendEmailConfirmation.
..                                                    
+  functions[sendEmailConfirmation]: Successful delete
 operation.                                           

+  Deploy complete!                                   

Project Console: https://console.firebase.google.com/p
roject/food-ninja-mobile/overview                     
Hosting URL: https://food-ninja-mobile.firebaseapp.com
=== Deploying to 'food-ninja-mobile'...

i  deploying database, functions, hosting
Running command: npm --prefix "$RESOURCE_DIR" run lint

> functions@ lint C:\...\---local path---
> eslint .

+  functions: Finished running predeploy script.
i  database: checking rules syntax...
+  database: rules syntax for database food-ninja-mobile is valid
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...
i  functions: packaged functions (52.54 KB) for uploading
+  functions: functions folder uploaded successfully
i  hosting: preparing public directory for upload...
+  hosting: 4 files uploaded successfully
i  database: releasing rules...
+  database: rules for database food-ninja-mobile released successfully
i  functions: creating function sendEmailConfirmation...
+  functions[sendEmailConfirmation]: Successful create operation.

+  Deploy complete!

Project Console: https://console.firebase.google.com/project/food-ninja-mobile/overview
Hosting URL: https://food-ninja-mobile.firebaseapp.com

@Jerry函数不起作用,因为您已将此函数调用替换为自己的函数

functions.database.ref('/users/{uid}').onWrite
这一行是函数的触发器,因此很自然,函数现在不起作用

部署云功能时,请始终查看生成的日志。 例如:如果您现在部署旧代码,日志将显示“sendEmailConfirmation函数已创建”

如果更新同一功能的内容,它将记录“发送电子邮件确认功能已更新”

如果删除或删除该函数,它将记录, “发送电子邮件确认功能被删除

希望这有帮助

获得评论后编辑:

    exports.sendEmailConfirmation = functions.database.ref('/users/{uid}').onWrite((event) => {
          const snapshot = event.data;
          const val = snapshot.val();

          if (!snapshot.changed('subscribedToMailingList')) {
            return null;
          }

//instead of getting from the request, have the mail variables either in a firebase node or for now just hard code it. for email, get it from the variable val
    const mailSubject = "This is my mail subject";
          const mailHtmlBody ="this is a awesome mail";
          const mailRecipient = val.email;



          const mailOptions = {
            from: '"Food Ninja." <foodninjaapp@gmail.com>',
            to:  mailRecipient,
            subject: mailSubject,
            html: mailHtmlBody
          };




          return mailTransport.sendMail(mailOptions)
            .then(() => console.log(`New ${subscribed ? '' : 'un'}subscription confirmation email sent to:`, val.email))
            .catch((error) => console.error('There was an error while sending the email:', error));
        });
exports.sendmailconfirmation=functions.database.ref('/users/{uid}').onWrite((事件)=>{
const snapshot=event.data;
const val=snapshot.val();
如果(!snapshot.changed('subscribedtomailglist')){
返回null;
}
//不要从请求获取邮件变量,而是将邮件变量放在firebase节点中,或者现在只需硬编码即可。对于电子邮件,请从变量val获取
const mailSubject=“这是我的邮件主题”;
const mailtmlbody=“这是一封很棒的邮件”;
const mailRecipient=val.email;
常量邮件选项={
来自:“‘食物忍者’”,
收件人:邮件收件人:,
主题:邮件主题,
html:mailHtmlBody
};
return mailTransport.sendMail(邮件选项)
.then(()=>console.log(`New${subscribed?''un'}订阅确认电子邮件发送到:`,val.email))
.catch((error)=>console.error('发送电子邮件时出错:',error));
});

@Jerry函数不起作用,因为您已将此函数调用替换为自己的函数

functions.database.ref('/users/{uid}').onWrite
这一行是函数的触发器,因此很自然,函数现在不起作用

部署云功能时,请始终查看生成的日志。 例如:如果您现在部署旧代码,日志将显示“sendEmailConfirmation函数已创建”

如果更新同一功能的内容,它将记录“发送电子邮件确认功能已更新”

如果删除或删除该函数,它将记录, “发送电子邮件确认功能被删除

希望这有帮助

获得评论后编辑:

    exports.sendEmailConfirmation = functions.database.ref('/users/{uid}').onWrite((event) => {
          const snapshot = event.data;
          const val = snapshot.val();

          if (!snapshot.changed('subscribedToMailingList')) {
            return null;
          }

//instead of getting from the request, have the mail variables either in a firebase node or for now just hard code it. for email, get it from the variable val
    const mailSubject = "This is my mail subject";
          const mailHtmlBody ="this is a awesome mail";
          const mailRecipient = val.email;



          const mailOptions = {
            from: '"Food Ninja." <foodninjaapp@gmail.com>',
            to:  mailRecipient,
            subject: mailSubject,
            html: mailHtmlBody
          };




          return mailTransport.sendMail(mailOptions)
            .then(() => console.log(`New ${subscribed ? '' : 'un'}subscription confirmation email sent to:`, val.email))
            .catch((error) => console.error('There was an error while sending the email:', error));
        });
exports.sendmailconfirmation=functions.database.ref('/users/{uid}').onWrite((事件)=>{
const snapshot=event.data;
const val=snapshot.val();
如果(!snapshot.changed('subscribedtomailglist')){
返回null;
}
//不要从请求获取邮件变量,而是将邮件变量放在firebase节点中,或者现在只需硬编码即可。对于电子邮件,请从变量val获取
const mailSubject=“这是我的邮件主题”;
const mailtmlbody=“这是一封很棒的邮件”;
const mailRecipient=val.email;
常量邮件选项={
来自:“‘食物忍者’”,
收件人:邮件收件人:,
主题:邮件主题,
html:mailHtmlBody
};
return mailTransport.sendMail(邮件选项)
.then(()=>console.log(`New${subscribed?''un'}订阅确认电子邮件发送到:`,val.email))
.catch((error)=>console.error('发送电子邮件时出错:',error));
});

如何部署该功能?当您部署它时,您得到了什么输出?我已经更新了post。您如何部署该函数?部署它时得到的输出是什么?我已经更新了postHi Praveen,它确实帮助我理解了为什么在部署新代码时会在我的控制台中显示该函数已被删除。但我仍然不太清楚它是如何工作的,因为我确实在那里替换了一个新函数。您能告诉我如何更改函数或任何文档吗?请详细解释这些内容**我用控制台的回复更新了帖子。我用更新代码中的几行编辑了您的示例代码。试着运行这个,看看它是否能根据您的代码工作,这是否意味着我们不能更改现有的函数?我们可以做的是在同一个文件中导出另一个新函数?嗨,Praveen,它确实帮助我理解了为什么在部署新代码时,它会在我的控制台中显示该函数已被删除。但我仍然不太清楚它是如何工作的,因为我确实在那里替换了一个新函数。您能告诉我如何更改函数或任何文档吗?请详细解释这些内容**我用控制台的回复更新了帖子。我用更新代码中的几行编辑了您的示例代码。试着运行这个,看看它是否能根据您的代码工作,这是否意味着我们不能更改现有的函数?我们能做的就是在同一个文件中导出另一个新函数?