Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot read property 'insert' of undefined

This is my first Node.js program using the Mongo db. This is my code:

/** * Module dependencies. */

 var express = require('express')
 , routes = require('./routes')
 , user = require('./routes/user')
 , http = require('http')
 , path = require('path');
 var mongo = require("./routes/mongo");
 var session = require('express-session');
 var MongoClient = require('mongodb').MongoClient;
 var assert = require('assert');

 var monStore=require("connect-mongo")(session);
 var url = 'mongodb://localhost:27017/session';


 var app = express();

 app.configure(function(){
   app.set('port', process.env.PORT || 4000);
   app.set('views', __dirname + '/views');
   app.set('view engine', 'ejs');
   app.use(express.favicon());
   app.use(express.logger('dev'));
   app.use(express.bodyParser());
   app.use(express.methodOverride());
   app.use(app.router);
   app.use(express.static(path.join(__dirname, 'public')));
 });

 app.configure('development', function(){
   app.use(express.errorHandler());
 });

 app.get('/', routes.index);
 app.get('/users', user.list);
 app.post('/signup',function(req,res){
var email=req.param("email");
var fname=req.param("firstname");
var lname=req.param("lastname");
var password=req.param("password");
var url1="mongodb://localhost:27017/signup";
mongo.connect(url1, function(){
var db= mongo.collection('user');

db.user.insert({"username": email,
                "password":password,
                "firstname":fname,
                "lastname":lname});

res.render("login",{title:"welcome"});
 });
 });

 app.post('/login',function(req,res){
    var email=req.param("email");
var password=req.param("password");
var url1="mongodb://localhost:27017/login";;
mongo.connect(url1, function(){
var db= mongo.collection('signup');
db.findOne({username: email, password:password}, function(err,user){
    console.log(user.email);
    if(user)
        {
        res.render("welcome");
        }
    else
        {
        res.render("login",{title:"ivalid"});
        }
});
});
 });

 MongoClient.connect(url, function() {

  console.log("Connected correctly to server.");

});
 http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
 });

i get the error Cannot read property 'insert' of undefined where i am trying to insert values into the database. Am I missing some point here? Can somebody Please help me out?

like image 316
sachin hunur Avatar asked Dec 24 '22 13:12

sachin hunur


1 Answers

You are using db variable as a reference to the collection:

var db= mongo.collection('user');

db.user.insert({"username": email,
...

The collection has no attribute called user, so that calling insert on it results in your error.

I believe that you wanted to do this:

var userCollection = var db= mongo.collection('user');

userCollection.insert({"username": email,
...
like image 129
slomek Avatar answered Jan 14 '23 19:01

slomek