2016-12-23 70 views
2

,我也面臨着.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 
}; 
+0

可以請你登錄「依賴」?因爲它看起來像依賴路徑不正確。否則代碼似乎很好。 – jimish

回答

0

您甲腎上腺素編輯轉換架構成一個模型,你可以工作,進入你的clientUser.js所以它傳遞給mongoose.model(MODELNAME,架構)

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

你的問題是/models/clientUser.js,要導出的模式。我的建議是導出模塊的模型。另外我不明白提出的dependency.js,爲什麼不直接在外接處理機的模式?下面是一個應該工作的建議。

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