NodeJS - .save() is not a function. Unable to create collection via mongoose

2.1k views Asked by At

I am also facing .save() is not a function error, but I have gone through most of the similar SO Questions and tried their respective answers but to vain. But through my terminal, I am being able to create the collection and save one. Also .find() is working too. Below is my code:

Model -> clientUser.js

var mongoose = require('mongoose');

var ClientUser = new mongoose.Schema({

    firebaseUID :{type: String},
    userName : { type : String},
    displayName : { type : String}

});
ClientUser.index({firebaseUID: 1}, {unique: true});
module.exports = {
    ClientUser : ClientUser
};

dependency.js

'use strict';
var mongoose = require('mongoose');
var ClientUser = mongoose.model('ClientUser');
var getNewClientUserModel = function () {
  return mongoose.model('ClientUser');
};
module.exports = {
        getNewClientUserModel : getNewClientUserModel
};

clientUser -> add.js

'use strict';
var dependency = require('./../../dependency');
var _ = dependency.getLodash();

var add = function (request, response) {
    var firebaseUID = request.body.firebaseUID;
    var userName = request.body.userName;
    var displayName = request.body.displayName;
    var newClientUser= dependency.getNewClientUserModel();
    newClientUser.firebaseUID = firebaseUID;
    newClientUser.userName = userName;
    newClientUser.displayName = displayName;

    if (!firebaseUID || !userName) {
        response.send(400);
        return null;
    } else {
        newClientUser.save(function (error,doc) {
            if (!_.isEmpty(error)) {
                response.send(500, {'message': 'Error in adding the Category', error: error});
            } else {
                response.send(200);
            }
            return null;
        });
    }
};

module.exports = {
    add: add
};

app.js

var mongoose = require('mongoose');
mongoose.model('ClientUser', require('./models/clientUser').ClientUser);
/*
 *Routes importing
 */
var ClientUser = {
    'add' : require('./routes/clientuser/add').add
};
2

There are 2 answers

0
cesar andavisa On BEST ANSWER

You need convert the schema into a Model you can work with, into your clientUser.js so pass it to mongoose.model(modelName, schema):

module.exports = mongoose.model('ClientUser', ClientUser);
0
rckrd On

Your problem is in /models/clientUser.js, you are exporting the schema. My suggestion is to export a model form that module. Also I don´t understand the propose of dependency.js, why not use the model directly in your add handler ? Below is an suggestion that should work.

models/clientUser.js

var mongoose = require('mongoose');

var clientUserSchema = new mongoose.Schema({
    firebaseUID :{type: String},
    userName : { type : String},
    displayName : { type : String}
});
clientUserSchema.index({firebaseUID: 1}, {unique: true});
var ClientUser = mongoose.model('ClientUser', clientUserSchema);
module.exports = {
    ClientUser : ClientUser
};

routes/clientuser/add.js

'use strict';
 var ClientUser = require('./clientUser').ClientUser;

 var add = function (request, response) {
     var firebaseUID = request.body.firebaseUID;
     var userName = request.body.userName;
     var displayName = request.body.displayName;
     var newClientUser = new ClientUser({
         firebaseUID: firebaseUID,
         userName: userName,
         displayName: displayName
     });

     if (!firebaseUID || !userName) {
       return response.send(400);
      }
      newClientUser.save(function (error, doc) {
         if (error) {
             return response.send(500, { 'message': 'Error in adding the Category', error: error });
          }
          return response.sendStatus(200);
      });

    };

module.exports = {
  add: add
};

app.js

/*
 *Routes importing
 */
var ClientUser = {
    'add' : require('./routes/clientuser/add').add
};