3gwebtrain
3gwebtrain

Reputation: 15293

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 );

Upvotes: 24

Views: 62835

Answers (4)

Abhinav Anand
Abhinav Anand

Reputation: 11

I am writing this for others who may encounter this problem. In my case I forgot to write await when trying to get the post model instance. In my case it is post instead of user

I wrote:

let post = Post.findById(req.body.post); //I forgot to write await
    if(post){
        let comment = await Comment.create({
            content: req.body.content,
            post: req.body.post, 
            user: req.user._id  
        });
        
        post.comments.push(comment);
        post.save();
        res.redirect('/');
        
    }
    else{
        console.log("post not found!!");
    }

Instead I should have written like this:

let post = await Post.findById(req.body.post);  //I used await here
    if(post){
        let comment = await Comment.create({
            content: req.body.content,
            post: req.body.post, 
            user: req.user._id  
        });
        
        post.comments.push(comment);
        post.save();
        res.redirect('/');
        
    }
    else{
        console.log("post not found!!");
    }

Upvotes: 1

Aditya
Aditya

Reputation: 1

Usermodel.collection.insertOne(values)

Upvotes: 0

raubas
raubas

Reputation: 724

You will need to also define a model, change your schema definition to this:

var UserSchema = new mongoose.Schema({
name:String,
username:{type:String, required:true, index:{unique:true}},
password:{type:String, required:true, select:false}
})
var User = mongoose.model('User', UserSchema);

Then you should be good to go :)

Upvotes: 2

vijay
vijay

Reputation: 631

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

Upvotes: 30

Related Questions