I'm making a script to add Q&A in react.js and mongodb. I have a problem when pressing a button creates the following errors
Failed to load resource: the server responded with a status of 404 (Not Found) create-quest.component.js:40 Object data: "↵↵↵↵Error↵↵↵
Cannot POST /create↵↵↵" status: 404 statusText: "Not Found" headers: {access-control-allow-origin: "*", connection: "close", content-length: "146", content-security-policy: "default-src 'none'", content-type: "text/html; charset=utf-8", …} config: {url: "http://localhost:3000/create", method: "post", data: "{"title":"aaa","content":"aaa"}", headers: {…}, transformRequest: Array(1), …} request: XMLHttpRequest {readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, onreadystatechange: ƒ, …} proto: Object
my code is:
import React, { Component } from 'react';
import axios from 'axios';
export default class CreateQuest extends Component {
constructor(props) {
super(props)
this.onChangeTitle = this.onChangeTitle.bind(this);
this.onChangeContent = this.onChangeContent.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.state = {
title: '',
content: ''
}
}
onChangeTitle(e) {
this.setState({ title: e.target.value })
}
onChangeContent(e) {
this.setState({ content: e.target.value })
}
onSubmit(e) {
e.preventDefault()
const questionObject = {
title: this.state.title,
content: this.state.content
};
axios.post('http://localhost:3000/create', questionObject)
.then(response => {
console.log(response)
})
.catch(error => {
console.log(error.response)
});
this.setState({ title: '', content: '' })
}
render() {
return (
<div className="wrapper">
<form onSubmit={this.onSubmit}>
<div className="form-group">
<label>Add title</label>
<input type="text" value={this.state.title} onChange={this.onChangeTitle} className="form-control" />
</div>
<div className="form-group">
<label>Add content</label>
<input type="text" value={this.state.content} onChange={this.onChangeContent} className="form-control" />
</div>
<div className="form-group">
<input type="submit" value="Create Question" className="btn btn-success btn-block" />
</div>
</form>
</div>
)
}
}
I am beginner in node react and mongo and I dont understand where is error
/create
route? - awarrier99app.js
orserver.js
file (or whatever you chose to call it). Can you update your post to include this file, as well as the snippets you posted as answers (and then remove the "answers" as well so others aren't confused by this page) - awarrier99