我正在使用Angular9+Firebase开发一个简单的联系人表单。我不知道如何从Firebase函数中的联系人表单中获取值。
我尝试了很多方法,但我总是得到";未定义的";价值以下是我的代码。如果有人知道怎么做,请给我解决方案。谢谢。
前端
import { AngularFireFunctions } from '@angular/fire/functions';
constructor(
private angularFireFunctions: AngularFireFunctions
) {}
submitContact(e: Event) {
e.preventDefault();
// Send Email Setup
const contactJson = JSON.stringify(this.contact);
const mailer = this.angularFireFunctions.httpsCallable('sendMail');
mailer(contactJson).subscribe(
(resp) => {
console.log({ resp });
},
(err) => {
console.error({ err });
}
);
return;
}
当点击提交按钮时,contactJson如下所示:
{"name":"Your Name","email":"youremailaddress@gmail.com","message":"Hello This is your message"}
Firebase函数-index.ts
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import * as nodemailer from 'nodemailer';
const cors = require('cors')({ origin: true });
admin.initializeApp();
const transporter = nodemailer.createTransport({
host: 'Your Host',
port: 465,
secure: true,
auth: {
user: 'Your Email Account',
pass: 'Your Password',
},
});
export const sendMail = functions.https.onRequest((request, response) => {
const name = request.body.name;
const email = request.body.email;
const message = request.body.message;
return cors(request, response, () => {
const text = `
Name - ${name}
Email - ${email}
Message - ${message}`;
const mailOptions = {
from: 'Your Email Address',
to: 'Customers Email Address',
subject: 'We received your inquiry',
text: text,
};
transporter.sendMail(mailOptions, (erro, info) => {
console.info(erro, info);
if (erro) {
response.send({ error: 'error' });
}
response.send({ data: 'success' });
});
});
});
以下是我收到的电子邮件正文。每个值都是"0";未定义";。
Name - undefined Email - undefined Message - undefined
在过程的最后,谷歌chrome控制台获得成功消息
{resp: "success"}
版本信息
Angular CLI: 9.0.7
Node: 10.15.3
OS: win32 x64
Angular: 9.0.7
... animations, cli, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: Yes
Package Version
-----------------------------------------------------------
@angular-devkit/architect 0.900.7
@angular-devkit/build-angular 0.900.7
@angular-devkit/build-optimizer 0.900.7
@angular-devkit/build-webpack 0.900.7
@angular-devkit/core 9.0.7
@angular-devkit/schematics 9.0.7
@angular/fire 6.0.2
@ngtools/webpack 9.0.7
@schematics/angular 9.0.7
@schematics/update 0.900.7
rxjs 6.5.5
typescript 3.7.5
webpack 4.41.2
收集信息的方式仅适用于application/json
的Content-Type
,因此首先要确定您是否确实在发送此特定内容类型,可以使用request.get('content-type')
:
export const sendMail = functions.https.onRequest((request, response) => {
response.send(req.get('content-type'));
)};
或者在emcompasses almos所有类型的HTTP请求的文档中使用此示例:
const escapeHtml = require('escape-html');
/**
* Responds to an HTTP request using data from the request body parsed according
* to the "content-type" header.
*
* @param {Object} req Cloud Function request context.
* @param {Object} res Cloud Function response context.
*/
exports.helloContent = (req, res) => {
let name;
switch (req.get('content-type')) {
// '{"name":"John"}'
case 'application/json':
({name} = req.body);
break;
// 'John', stored in a Buffer
case 'application/octet-stream':
name = req.body.toString(); // Convert buffer to a string
break;
// 'John'
case 'text/plain':
name = req.body;
break;
// 'name=John' in the body of a POST request (not the URL)
case 'application/x-www-form-urlencoded':
({name} = req.body);
break;
}
res.status(200).send(`Hello ${escapeHtml(name || 'World')}!`);
};
唯一似乎缺少的类型是Multipar-Form
,这种特定类型可以在文档中使用此示例来完成