NodeJS – .save()不是一个函数。 无法通过mongoose创build收集

我也面临.save()不是一个函数错误 ,但我已经经历了大多数类似的SO问题,并尝试各自的答案,但徒劳无功。 但通过我的terminal,我可以创buildcollections并保存一个。 另外.find()也在工作 。 以下是我的代码:

模型 – > 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 }; 

       

网上收集的解决方案 "NodeJS – .save()不是一个函数。 无法通过mongoose创build收集"

您需要将模式转换为您可以使用的Model,放入您的clientUser.js中 ,然后将其传递给mongoose.model(modelName,schema)

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

你的问题在/models/clientUser.js ,你正在导出模式。 我的build议是导出一个模型的forms,该模块。 另外我不理解dependency.js的build议,为什么不直接在你的添加处理程序中使用模型? 下面是一个应该工作的build议。

车型/ 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 }; 

路由/ 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 };