Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose getting `TypeError: user.save is not a function` - what is wrong

I am trying to post the user data to mongodb but I am getting an error as :

`TypeError: user.save is not a function' - how to fix this?

here is my code :

var

    express     = require('express'),
    app         = express(),
    bodyParser  = require('body-parser'),
    mongoose    = require('mongoose'),
    PORT        = process.env.PORT || 8080;



//connect to db 

mongoose.connect('mongodb://3gwebtrain:[email protected]:47975/family');

app.use( bodyParser.urlencoded({extended : true }));
app.use( bodyParser.json() );


app.get('/', function( req, res ) {

    res.json({message:'works'})

});


var Schema = mongoose.Schema;

var User = new Schema({

    name:String,
    username:{type:String, required:true, index:{unique:true}},
    password:{type:String, required:true, select:false}

})


var apiRouter = express.Router();


apiRouter
    .get('/users', function( req, res ){

        res.send( "yet to get users!");

    })
    .post('/users', function( req, res ) {

        var user = User;

        user.name = req.body.name;
        user.username = req.body.username;
        user.password = req.body.password;

        user.save(function(err) {

            if( err ) {

                console.log( err ); //TypeError: user.save is not a function

            }

            res.send("user created!");

        })

    })


app.use('/api', apiRouter);


app.listen( PORT );
console.log( 'app listens at ' + PORT );
like image 706
3gwebtrain Avatar asked Nov 01 '16 06:11

3gwebtrain


People also ask

What is Save function in Mongoose?

Mongoose | save() Function The save() function is used to save the document to the database. Using this function, new documents can be added to the database.

How do I get the Objectid after I save an object in Mongoose?

To get the object ID after an object is saved in Mongoose, we can get the value from the callback that's run after we call save . const { Schema } = require('mongoose') mongoose. connect('mongodb://localhost/lol', (err) => { if (err) { console. log(err) } }); const ChatSchema = new Schema({ name: String }); mongoose.

Does update call save in Mongoose?

The main difference is that with . save() you already have an object in your client side code or had to retrieve the data from the server before you are writing it back, and you are writing back the whole thing. On the other hand . update() does not require the data to be loaded to the client from the server.

Does Mongoose save overwrite?

Mongoose save with an existing document will not override the same object reference. Bookmark this question.


1 Answers

first create model from Schema :

 var UserModel = mongoose.model('User', User);

then create object out of User model

 var user = new UserModel(req.body)

then call

 user.save(function(){})

check documentation http://mongoosejs.com/docs/api.html#model_Model-save

like image 188
vijay Avatar answered Oct 18 '22 20:10

vijay