I am having trouble receiving a response from my backend (Node.js Express RestAPI).
exports.send = function(req, res) {
console.log("sent function running!")
let contact = new Contact(req.body)
contact.send()
if (contact.errors.length) {
res.status(400).send(JSON.stringify(contact.errors));
return;
}
res.send("Message Sent!")
}
The above will send the errors that were encountered via res.status(400) line.
When I use PostMan to send a POST method I receive the correct response messages. see below.
However when I use Axios on my frontend I am not receiving a response
FrontEnd
const axios = require('axios')
class ContactForm {
constructor() {
this.name = document.getElementById("fname").value;
this.email = document.getElementById("email").value;
this.message = document.getElementById("message").value;
this.submitButton = document.getElementById("submitMessage")
this.events();
}
events() {
this.submitButton.addEventListener("click", (e) => this.sendMesage(e))
}
sendMesage(e) {
e.preventDefault()
axios.post('http://localhost:5000/api/contact/send', {
name: this.name,
email: this.email,
message: this.message
}).then(res => {
console.log(res)
}).catch(e => {
console.log("ran into errors")
console.log(e)
})
}
}
export default ContactForm


