I am self learning nodeJS, Now I tried to insert data in MongoDB and this my goal
insert values in here and once submit button is clicked, should save the data successfully to mongodb and this should return a successful message.
But this is the error
TypeError: Cannot read property 'location' of undefined
Here are the code snippets
const express = require('express');
const dotenv = require("dotenv").config();
const address = process.argv[2];
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const app = express();
//INSERT TO MONGO DB
//connect to mongo db
mongoose.connect('mongodb://localhost/weathertest2');
mongoose.Promise = global.Promise;
//create weather schema
const WeatherSchema = new Schema({
location:{
type: String
},
temperature:{
type: String
},
observationTime:{
type: String
}
});
const Weather = mongoose.model('weather', WeatherSchema);
// post request
app.post('/new', function(req, res){
new Weather({
location : req.body.location,
temperature: req.body.temperature,
observationTime : req.body.observationTime
}).save(function(err, doc){
if(err) res.json(err);
else res.send('Successfully inserted!');
});
});
// listen for request
app.listen(process.env.port || 9000, function(){
console.log('now listening for testing request');
});
app.use(express.static('public'));
