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
};
You need convert the schema into a Model you can work with, into your clientUser.js so pass it to mongoose.model(modelName, schema):