data is not insert in mongodb - node.js

I am beginner of nodejs and mongodb. I am inserting data to collection using mongoose ORM and model but not insert. Validation is working correct but data is not insert after filling complete data. I have not create collection in database manually. should I create collection manually in mongodb or create automatically when inserting document.
productsController
var mongoose = require('mongoose');
var db = require('../config/db_config');
var Product = require('../models/product');
//var Product = mongoose.model('Products');
var productController = {};
productController.add = function(req, res) {
var params = req.body;
if(!params) {
res.status(400).send({message: "Data can not be empty"});
}
var productData = new Product({
product_name : params.product_name,
price : params.price,
category : params.category
});
console.log(productData);
productData.save(function(err, product) {
if (err){
res.status(400).send({message:'Unable to save data! ' + err});
}else{
res.status(200).send({message:'Data has been saved! ' + product });
}
});
};
module.exports = productController;
Models code is here
var mongoose = require('mongoose');
var db = require('../config/db_config');
var Schema = mongoose.Schema;
var productSchema = new Schema({
product_name: { type: String, required: 'Product name cannot be left blank.' },
price: { type: String, required: 'Product price cannot be left blank.'},
category: { type: String , required: 'Product category cannot be left blank'},
updated_at : {type: Date, default: Date.now}
});
module.exports = mongoose.model('Products', productSchema);
routes file code is here:
var express = require('express');
var router = express.Router();
var mongoose = require('mongoose');
var Product = require('../models/product.js');
var productsController = require('../controllers/productsController');
router.post('/add',productsController.add);
module.exports = router;
DB config file
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
var db = mongoose.createConnection('mongodb://localhost:27017/nodeweb', function(err,db){
if(err){
throw err;
console.log(err);
} else {
console.log('Successfully connected to database!');
}
});
module.exports = db;
I have insert controller, model and routes file code.

Please correct your method -
Controller function
exports.add = function(req, res) {
var new_product = new Products(req.body);
// you have define Products not Product
console.log(new_product);
new_product.save(function(err, product) {
console.log('add data');
if (err){
res.send(err);
}else{
res.json(product);
}
});
};
and for good practice in node js - i think you can start with express-app-generator
This Helps to make simple routing, with generic responses and in-built express middlewares with loggers.

I have resolved problem. database connection work with connect method.
mongoose.connect('mongodb://localhost:27017/nodeweb', {useMongoClient: true}, function(err,db){
if(err){
throw err;
console.log(err);
} else {
console.log('Successfully connected to database!');
}
});

Related

Node.js: indexOf returning -1 even though item is in array

I want users to have the ability to click a button that pushes their username and id into an array associated with a collection in a database, but only if they're not already in that array.
My solution is:
var isInGroup = function(user, arr){
var match = arr.indexOf(user);
console.log(">>>>>>>" + match);
if(match === -1){
arr.push(user);
console.log("added user");
} else {
console.log("Already in group");
}
};
This works when I test it against example arrays in the console, but not when I'm querying the database. When I execute the function in my app, arr.indexOf = -1 even if the user is already in the array.
This is the relevant code:
Player.js
var express = require("express"),
router = express.Router({mergeParams:true}),
Game = require("../models/game"),
Player = require("../models/player"),
User = require("../models/user"),
middleware = require("../middleware");
//Add A Player
router.post("/", middleware.isLoggedIn, function(req, res){
//find game
Game.findById(req.body.game, function(err, foundGame){
console.log(">>>>" + foundGame);
if(err){
req.flash("error", "Something went wrong.");
} else {
//create player
Player.create(req.user, function(err, player){
if(err){
console.log(">>>> error" + player);
res.redirect("back");
} else {
player.id = req.user_id;
player.username = req.user.username;
middleware.isInGroup(player, foundGame.players);
foundGame.save();
res.redirect("back");
}
});
}
});
});
Game Schema
var mongoose = require("mongoose");
var gameSchema = new mongoose.Schema({
name:String,
author:{
id:{
type: mongoose.Schema.Types.ObjectId,
ref:"User"
},
username:String,
},
court:{
id:{
type:mongoose.Schema.Types.ObjectId,
ref:"Court"
},
name:String,
},
players:[
{
id:{ type:mongoose.Schema.Types.ObjectId,
ref:"Player",
},
username:String
}
],
time:{
start:String,
end:String
},
date:String,
});
module.exports = mongoose.model("Game", gameSchema)
Player Schema
var mongoose = require("mongoose");
var playerSchema = new mongoose.Schema({
id:{type:mongoose.Schema.Types.ObjectId,
ref:"User"
},
username: String
});
module.exports = mongoose.model("Player", playerSchema);
User Schema
var mongoose = require("mongoose"),
passportLocalMongoose = require("passport-local-mongoose");
var userSchema = new mongoose.Schema({
username: String,
password: String
});
userSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", userSchema);
As mentioned above, arr.indexOf(user) returns -1 even if user is already in the array. Why is this happening? Is there better solution to this problem? Thanks for the help. I've been banging my head for awhile on this one.

Mongoose, can't print products from database

I have problem with print items in console.log or res.json from database.
what am I doing wrong
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
var Schema = mongoose.Schema;
var productSchema = new Schema({
title: String,
price: Number
});
var product = mongoose.model('product, productSchema');
mongoose.connect(db);
var db = 'mongodb://localhost/product';
router.get('/books', function(req, res) {
console.log('getting all products');
product.find({})
.exec(function(err, product) {
if (err) {
res.send('errror');
} else {
console.log(product);
res.json(product);
}
})
});
name of database products is : db.product
Thanks
Error:
C:\Users\Turqus\Desktop\node\products\node_modules\mongoose\lib\index.js:382
throw new mongoose.Error.MissingSchemaError(name);
^
MongooseError: Schema hasn't been registered for model "product, productSchema".
Use mongoose.model(name, schema)
at Mongoose.model (C:\Users\Turqus\Desktop\node\products\node_modules\mongoose\lib\index.js:382:13)
You have to make the Database Connection first.
change this
var product = mongoose.model('product, productSchema');
to
var product = mongoose.model('product', productSchema);
mongoose.model() accepts two parameter the name of the collection your model is for and the 2nd the Schema
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
var Schema = mongoose.Schema;
var productSchema = new Schema({
title: String,
price: Number
});
var product = mongoose.model('product', 'productSchema');
router.get('/books', function(req, res) {
console.log('getting all products');
product.find({})
.exec(function(err, product) {
if (err) {
res.send('errror');
} else {
console.log(product);
res.json(product);
}
})
});

node server crashing when validation error occurs with mongoose

I have created a dynamic schema for every user with node and mongoose as given below.
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
require('mongoose-currency').loadType(mongoose);
var Currency = mongoose.Types.Currency;
var values = 'Credit Debit'.split(' ');
var schema = new Schema({
amount: {
type: Currency,
required: true
},
type: {
type: String,
enum: values
},
description: {
type: String,
required: true
}
},
{
timestamps: true
});
exports.create_database = function(mobile) {
return mongoose.model('personal_'+mobile, schema, 'personal_'+mobile);
};
I have used this in a personal router code given below
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var database = require('../models/personal');
var Verify = require('./verify');
var personalRouter = express.Router();
personalRouter.use(bodyParser.json());
personalRouter.route('/')
.get(Verify.verifyOrdinaryUser, function (req, res, next) {
var mobile = req.decoded._doc.mobile;
var Personal = database.create_database(mobile);
Personal.find(req.query)
.exec(function (err, personal) {
if (err) next(err);
res.json(personal);
})
})
.post(Verify.verifyOrdinaryUser, function(req, res, next) {
var mobile = req.decoded._doc.mobile;
var Personal = database.create_database(mobile);
Personal.create(req.body, function(err, personal) {
if(err) next(err);
console.log('Personal Record added!');
var type = personal.type;
var amount = personal.amount/100;
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Added the '+type+' record of amount = '+amount);
})
})
module.exports = personalRouter;
Now everything works good withe the get and the post routers untill i pass in values which do not go with the mongoose schema.
Eg. If i pass in the value as say without a description my server will give a validation error and crash.
I tried replacing next(err) with throw(err in the code without any luck.
I want the error to be displayed and prevent the server from crashing and shutting down.
Please help me, where am i going wrong?

express server api returns empty

i am trying to get the data from mongodb using express server but all i am getting is empty array => []
However if i run the db.Goserv.find() in console i get the results right please help
here is the server.js file
var Schema = mongoose.Schema;
var schema = new Schema({
type: String,
address: String,
servicecost: String
}, { collection: 'Goserv' });
var Goserv = mongoose.model('Goserv', schema );
module.exports = Goserv ;
app.get('/api/centre', function(req, res) {
Goserv.find(function(err, centre){
if(err){
res.send(err);
} else {
res.json(centre);
console.log(centre);
}
});
});
Try this...
var Schema = mongoose.Schema;
var schema = new Schema({
type: String,
address: String,
servicecost: String
}, { collection: 'Goserv' });
var Goserv = mongoose.model('Goserv', schema );
module.exports = Goserv ;
app.get('/api/centre', function(req, res) {
Goserv.find({},function(err, centre){
if(err){
res.send(err);
} else {
res.json(centre);
console.log(centre);
}
});
});

reference and populate issue

[Solved - silly mistake in the model]
I have been trying to refer and populate in mongoose and expressjs, but its not working at all. I have followed these:
1. Mongoose examples
2. Mongoose populate
I created three files:
1) models/profile.js
var mongoose = require('mongoose'), Schema = mongoose.Schema, ObjectId = Schema.ObjectId;
var profileSchema = new Schema({
name: String
});
module.exports = mongoose.model('Profile', profileSchema);
2) models/work.js
var mongoose = require('mongoose'), Schema = mongoose.Schema, ObjectId = Schema.ObjectId;
var workSchema = new Schema({
title: String,
creditsFor: [{type: Schema.Types.ObjectId, ref: 'profile'}],
});
module.exports = mongoose.model('Work', workSchema);
3) controllers/work.js
var Work = require('../models/work.js'), fs = require('fs'), mongoose = require('mongoose');
......
exports.create = function(req, res) {
credits = [];
credits.push(mongoose.Types.ObjectId('5174a9ec993af25b01000003')); // I already created a 'Profile' and copy-pasted the _id
var work = {
title: req.body.title,
creditsFor: credits
}
var workObj = new Work(work);
workObj.save(function(err, data) {
if(err) {
throw err;
} else {
req.flash('info', 'Work item saved.');
res.redirect('/work/' + data._id);
}
});
}
.......
exports.show = function(req, res) {
var work = req.params.work;
Work.findOne({_id: work})
.populate('creditsFor')
.exec(function(err, data) {
if(err) {
throw err;
} else {
console.log(data);
res.render('site/work/show', {work: data, message: req.flash('info')});
}
});
}
When using populate(), returned creditsFor is null. When I comment populate(), it is a string(?), like this:
{
title: 'My Title',
creditsFor: '5174a9ec993af25b01000003'
}
Couldn't figure out whats going wrong. I tried to bring both the models in a single file, still nothing.

Resources