My HTML Code
<body>
<form action="/contact" method="POST" id="contact">
<div class="form">
<div><input type="text" name="myname" placeholder="Type your name" required></div>
<div><input type="text" name="city" placeholder="City" required></div>
<div><input type="number" name="number" placeholder="Type your phone number" required></div>
<div><input type="email" name="email" placeholder="ex:mohd53635@gmail.com" required></div>
<div><input type="number" name="age" placeholder="Age" min="17" max="40" required></div>
<div> Male<input type="radio" name="gender" id="male">Female <input type="radio" name="gender" id="female"></div>
<button>Submit</button>
<button type="reset">Reset</button>
</div>
</form>
</body>
sending an post request with mongoose but data is not saving in database I don't even getting error...
JavaScript modules:-
const mongoose = require('mongoose');
const { ejson } = require("ejs");
const express = require("express");
const app = express();
const path = require('path')
const fs = require("fs");
const bodyParser = require("body-parser")
const { urlencoded } = require("express");
const port = 5000;
const hostname = '127.0.0.1';
//middlewares
app.use('/static', express.static(path.join(__dirname, "../static")));
app.use('/static', express.static('static'))
app.use(express.static(__dirname + '/static'));
//HTTP GET Requests:-
app.get('/index.html', function(req, res) {
res.status(200).render(__dirname + '/index.html');
})
app.get('/contact.html', function(req, res) {
res.sendFile(__dirname + '/contact.html')
});
app.engine('html', require('ejs').renderFile);
//Server
app.listen(port, () => {
// console.log(`The Server Has Been Started On Port ${port}`)
});
//MongoDb Mongoose
mongoose.connect('mongodb://localhost/moonform', {
useNewUrlParser: true,
useUnifiedTopology: true
});
let db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log("We are connected");
//Mongoose Schema:-
const moonSchema = new mongoose.Schema({
myname: String,
number: Number,
email: String,
city: String,
age: Number,
gender: String
});
//Mongoose Model
let moon = mongoose.model('moon', moonSchema);
//Post Request
app.post('/contact', (req, res) => {
let myData = new moon(req.body)
myData.save().then((err, myData) => {
res.send("This item has been saved to the database")
})
.catch(() => {
res.status(400).send("item was not saved to the databse")
});
});
//Mongoose Find
moon.find({ name: "moonform" }, function(err, myData) {
if (err) return console.error(err);
console.log(moon)
});
Ok. Just try this.
//don't forget to wrap this inside async function
const result=await moon.find({});
console.log(result)
If this still gives an error that means your req.body
might be empty
well i finally figured out my problem that only thing that is missing in the code was..
app.use(express.urlencoded());
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.