,我也面臨着.save()創建收集不是一個函數錯誤,但我已經通過大多數類似的走了這麼問題,並試圖各自的答案,但徒勞無功。但通過我的終端,我能夠創造的收集和保存的。此外.find()正在了。下面是我的代碼:的NodeJS - .save()不是一個函數。無法通過貓鼬
型號 - > 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
};
可以請你登錄「依賴」?因爲它看起來像依賴路徑不正確。否則代碼似乎很好。 – jimish