Here is how I am receiving a response from an Express API call I am making from my Angular 2 application. In my component:
this.emailService.sendEmail(this.name, this.email, this.message)
.subscribe(
(res) => {
console.log("Success");
this.success = true;
},
(err) => {
console.log("Failure");
this.success = false;
}
);
In my email service sendEmail() function:
return this.http.post('/api/sendemail', data)
.map(res => res.json());
From my Express API, I am sending the email and sending a response like so with the emailjs node module:
router.post('/sendemail/', (req, res) => {
...
server.send({
text: "Sent from " + req.body.name + " message: " + req.body.message + " reply to: " + req.body.email,
from: "email@gmail.com",
to: "email@gmail.com",
subject: "test"
}, function(err, message) {
if (err) {
console.log(err);
res.status(500).send("Email failed to send.");
} else if (message) {
console.log(message);
res.status(200).send("Email sent successfully.");
}
});
});
The email is being successfully sent every time, and a 200 status code is being returned (I see the response in my console and the Heroku console), but the console logs "Failure" regardless. Am I properly sending the response back to my application?
Change:
res.status(200).send("Email sent successfully.");
To:
res.status(200).send({message: "Email sent successfully."});
or
res.status(200).json({message: "Email sent successfully."});
or
res.status(200);
Try some thing like this.
res.status(200).json({
message: 'Email sent successfully.'
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.