Reputation: 1409
I am new to Node.js and I know this is a very common question, and lot of people have asked the same question. But you know what none of the solutions are working in my case.
Please take a look at the code, and let me know if I missed some thing
Thanks you!!
this is node.js application interacting with Mongo db.
Product.controllers.js
const Products = require("../models/Product.models");
exports.Product_create = function(req, res, next){
console.log(req.body.Name); //For test only - Here I am getting the values
console.log(req.body.Age); //For test only - Here I am getting the values
let newProduct = new Products()
{
first_name = req.body.Name,
age = req.body.Age
};
newProduct.save(function (err) {
if (err) {
return next(err);
}
res.send('Product Created successfully')
})
};
exports.test = function(req, res){
console.log("test controller reached successfully!");
};
Product.models.js
var mongoose = require("mongoose");
var schema = mongoose.Schema;
let ProductSchema = new schema({
first_name : {type: String, required: true, max: 100},
age: {type:Number, required: true}
});
//export model
module.exports = mongoose.model("Product", ProductSchema);
this is the request I am sending.
I am receiving correct output in console.log(req.body.Name);
and console.log(req.body.Age);
. But still it is not saving the data.
Upvotes: 0
Views: 476
Reputation: 725
The error is in your product initialization
const Products = require("../models/Product.models");
exports.Product_create = function(req, res, next){
console.log(req.body.Name); //For test only - Here I am getting the values
console.log(req.body.Age); //For test only - Here I am getting the values
let newProduct = new Products(
{
first_name:req.body.Name,
age:req.body.Age
});
newProduct.save(function (err) {
if (err) {
return next(err);
}
res.send('Product Created successfully')
})
};
exports.test = function(req, res){
console.log("test controller reached successfully!");
};
you need to provide the first_name and age
as key value pairs
Upvotes: 1