Search code examples
angularjsnode.jsmongodbexpressmongoose-schema

TypeError: Dbschema(Mongoose Schema) is not a function


I am trying to create a simple registration form using MEAN_Stack using mongoose. Here is my models/dbSchema.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var User = new mongoose.Schema({
   FirstName: String,
   LastName:  String,
   City    :  String,
   Email   :  String,
   Userid  :  String,
   Password:  String
});
module.export = mongoose.model('user', User);

and this is my server.js

var express = require('express');
var app =  express();
var bodyParser = require('body-parser');
var jwt = require('jsonwebtoken');

app.use(express.static(__dirname + "/public"));

// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/Regis_module');
var Userschema = require('./models/dbSchema'); 

app.post('/regi',function(req,res){
    var schema        =  new Userschema();
    schema.Firstname      =  req.body.Fname;
    schema.Lastname      =  req.body.Lname;
    schema.City       =  req.body.city;
    schema.Email      =  req.body.email;
    schema.Userid     =  req.body.userid;
    schema.Password   =  req.body.password;
     
    schema.save(function(err) {
        if (err)
            res.send(err);

        res.json({ message: 'Record Inserted', Firstname: req.body.firstname, Lastname: req.body.lastname, city:req.body.city, email:req.body.email, 
                   userid:req.body.userid, password :req.body.password /*, fbId : req.body.fbId*/ });
    });        
});
    
app.listen(3000);
console.log("listening to port 3000");

While running on the local host, at the time of form submission, Firstname and Lastname are not stored in the database. City, Eamil, Userid and Password are storing correctly.

How can I store all things in database correctly? Please help me?


Solution

  • models/dbSchema.js

    It's module.exports not module.export. And also no need to write var Schema = mongoose.Schema;. There is no use of Schema variable in your code.

    server.js

    var schema = new Dbschema({
      FirstName  :  req.body.Fname,
      LastName   :  req.body.Lname,
      City       :  req.body.city,
      Email      :  req.body.email,
      Userid     :  req.body.userid,
      Password   :  req.body.password
    });
    

    or,

    var schema = new Dbschema();
    
    schema.FirstName  =  req.body.Fname;
    schema.LastName   =  req.body.Lname;
    schema.City       =  req.body.city;
    schema.Email      =  req.body.email;
    schema.Userid     =  req.body.userid;
    schema.Password   =  req.body.password;
    

    Let me know if you have any problem understanding this. I will send you full code snippet if needed.

    Thanks!