1handleSubmit(){
2 let databody = {
3 "name": this.state.nameIn,
4 "quote": this.state.quoteIn
5 }
6
7 return fetch('http://localhost:5002/stored', {
8 method: 'POST',
9 body: JSON.stringify(databody),
10 headers: {
11 'Content-Type': 'application/json'
12 },
13 })
14 .then(res => res.json())
15 .then(data => console.log(data));
16}
17
18
19render(){
20 return (
21 <div>
22 <form onSubmit={this.handleSubmit}>
23 <label>
24 Name
25 <input type="text" name="name" value={this.nameIn} onChange={this.handleNameChange}/>
26 </label>
27 <label>
28 quote
29 <input type="text" name="quote" value={this.quoteIn} onChange={this.handleQuoteChange}/>
30 </label>
31 <input type="submit" value="Add to DB" />
32 </form>
33 </div>
34 );
35}
36
1app.post('/stored', (req, res) => {
2 console.log(req.body);
3 db.collection('quotes').insertOne(req.body, (err, data) => {
4 if(err) return console.log(err);
5 res.send(('saved to db: ' + data));
6 })
7});
8