首页 > 解决方案 > 使用 Cloud Functions 在 Firestore 上创建用户时发送电子邮件

问题描述

在我的颤振应用程序上创建用户后,我尝试发送电子邮件验证链接,但未发送电子邮件,并且在我的 Cloud Functions 日志中,我在部署时收到了消息:

{"@type":"type.googleapis.com/google.cloud.audit.AuditLog","status":{"code":9,"message":"FAILED_PRECONDITION"},"authenticationInfo":{"principalEmail" :"*************"},"requestMetadata":{"callerIp":"186.216.140.62","callerSuppliedUserAgent":"FirebaseCLI/6.5.0,gzip(gfe),gzip (gfe)","re​​questAttributes":{"time":"2019-03-29T23:21:10.130Z","auth":{}},"destinationAttributes":{}},"serviceName":"cloudfunctions. googleapis.com","methodName":"google.cloud.functions.v1.CloudFunctionsService.UpdateFunction","authorizationInfo":[{"permission":"cloudfunctions.functions.update","granted":true,"resourceAttributes":{}},{"resource":"projects/pppppp-9800a/locations/us-central1/functions/sendVerificationEmail","permission":"cloudfunctions.functions.update","granted":true,"resourceAttributes ":{}}],"resourceName":"projects/pppppp-9800a/locations/us-central1/functions/sendVerificationEmail","re​​quest":{"@type":"type.googleapis.com/google.cloud. functions.v1.UpdateFunctionRequest","function":{"labels":{"deployment-tool":"cli-firebase"},"eventTrigger":{"eventType":"providers/cloud.firestore/eventTypes/document.创建","资源":"projects/pppppp-9800a/databases/(默认)/documents/users/{userId}","service":"firestore.googleapis.com"},"sourceUploadUrl":"https://storage.googleapis.com/gcf-upload-us-central1-dc1829cf-3a07-4951-be81-1a15f892ed8d/8ea3f162-c860-4846-9064-04a855efca2f.zip?GoogleAccessId=service-73683634264@gcf-admin- robot.iam.gserviceaccount.com&Expires=1553903464&Signature= ******************","name":"projects/pppppp-9800a/locations/us-central1/functions/sendVerificationEmail "}}}

我的代码:

exports.sendVerificationEmail = functions.firestore.document('users/{userId}').onCreate((snap, context) => {
  const user = snap.data();
  console.log("----------------------");
  console.log("user created: " + user.uidColumn);
  admin.auth().generateEmailVerificationLink(user.email).then((link) => {
    console.log("**********" + link);
    sendVerificationEmail(user.emailColumn, link);
    return 0;
  }).catch(e => {
    console.log(e);
  })
  return 0;
});

function sendVerificationEmail(email, link) {
  var smtpConfig = {
    host: 'smtp.gmail.com',
    port: 465,
    secure: true, // use SSL
    auth: {
      user: 'myappemail@gmail.com',
      pass: 'password'
    }
  };

  var transporter = nodemailer.createTransport(smtpConfig);

  var mailOptions = {
    from: "qeaapp@gmail.com", // sender address
    to: email, // list of receivers
    subject: "Email verification", // Subject line
    text: "Email verification, press here to verify your email: " + link,
    html: "<b>Hello there,<br> click <a href=" + link + "> here to verify</a></b>" // html body
  };

  transporter.sendMail(mailOptions, function (error, response) {

    if (error) {
      console.log(error);
    } else {
      console.log("Message sent: " + response.message);
    }
    return 0;
  });
  return 0;
}

当我执行命令时,firebase deploy我收到消息函数:未能更新函数 sendVerificationEmail HTTP 错误:400,不允许更改函数触发器类型或事件提供程序

我是 JS 新手,我不知道这些错误是什么意思

标签: firebasegoogle-cloud-functions

解决方案


删除第一个名为 的函数sendVerificationEmail,然后重新部署。看起来您最初可能将其部署为 Firestore 触发器以外的其他东西。


推荐阅读