2016-11-23 70 views
1

我試圖子文檔添加到但是我被拋出下面的錯誤與貓鼬和MongoDB父模式:貓鼬類型錯誤:用戶是不是構造

TypeError: User is not a constructor 

這是基於關閉貓鼬的文檔在subdocuments和我認爲一切都是一樣的。我怎樣才能進一步調試呢?

路由器

// Add a destination to the DB 
router.post('/add', function(req, res, next) { 
    let airport = req.body.destination 
    let month = req.body.month 
    let id = (req.user.id) 

    User.findById(id , function (err, User) { 
    if (err) return handleError(err) 

    function addToCart (airport, month, id) { 
     var user = new User ({ 
     destinations: [(
      airport = '', 
      month = '' 
     )] 
     }) 

     dog.destinations[0].airport = airport 
     dog.destinations[0].month = month 
     dog.save(callback) 
     res.status(200).send('added') 
    } 
    addToCart() 
    }) 
    console.log(airport) 
}) 

架構

var destinationSchema = new Schema({ 
    airport: String, 
    month: String 
}) 

// Define the scheme 
var User = new Schema ({ 
    firstName: { 
    type: String, 
    index: true 
    }, 
    lastName: { 
    type: String, 
    index: true 
    }, 
    email: { 
    type: String, 
    index: true 
    }, 
    homeAirport: { 
    type: String, 
    index: true 
    }, 
    destinations: [destinationSchema] 
}) 


User.plugin(passportLocalMongoose) 

module.exports = mongoose.model('User', User) 

回答

4

JavaScript是有關情況的變量名敏感。您有User型號和User結果具有相同的名稱。

您的代碼將有以下變化工作:

User.findById(id , function (err, user) { 
/*         ^use small `u` */ 
     if (err) return handleError(err) 

/* rest of your code */ 

而且記住,進一步在你的代碼您聲明命名user另一個變量。你需要將其改變爲不同的東西。

+0

我有同樣的問題。你的解決方案解決了我的問題 感謝分享 – AllJs