2016-11-14 19 views
-1

我需要在我的數據庫中插入'caso',但我得到一個錯誤,說caso.save is not a function。我已經嘗試了成千上萬種不同的插入方式,但都沒有成功。難道我做錯了什麼?我的caso.find(...)工作正常!保存不是一個函數

服務器端代碼:

app.post('/api/casos', function (req, res) { 
     var caso = new Caso(); 
     caso = req.body; 

     caso.save(function (err) { 
      if (err) { 
       res.send(err); 
      } else { 
       res.json({message: "Caso adicionado com sucesso!"}); 
      } 
     }); 
    }); 

控制器代碼:

$http.post('/api/casos', $rootScope.caso).success(function(res){ 
      console.log(res); 
     }); 

我的 'CASO' 模型:

var mongoose = require('mongoose'); 

module.exports = mongoose.model('Caso', { 
    caso: Number, 
    doenca: String, 
    areaDamaged: String, 
    cankerLesion: String, 
    cropHist: String, 
    date: String, 
    externalDecay: String, 
    fruitSpots: String, 
    fruitingBodies: String, 
    fruitPods: String, 
    germination: String, 
    hail: String, 
    intDiscolor: String, 
    leafMalf: String, 
    leafMild: String, 
    leafShread: String, 
    leafspotsHalo: String, 
    leafspotSize: String, 
    leafspotsMarg: String, 
    leaves: String, 
    lodging: String, 
    moldGrowth: String, 
    mycelium: String, 
    plantGrowth: String, 
    plantStand: String, 
    precip: String, 
    roots: String, 
    sclerotia: String, 
    seed: String, 
    seedDiscolor: String, 
    seedSize: String, 
    seedTmt: String, 
    severity: String, 
    shriveling: String, 
    stem: String, 
    stemCankers: String, 
    temp: String 
}, "casos"); 

的req.body JSON:

{ areaDamaged: 'low-areas', 
    cankerLesion: 'dk-brown-blk', 
    cropHist: 'same-1st-yr', 
    date: 'Abril', 
    externalDecay: 'Absent', 
    fruitSpots: 'dna', 
    fruitingBodies: 'Absent', 
    fruitPods: 'dna', 
    germination: '90-100%', 
    hail: 'Yes', 
    intDiscolor: 'None', 
    leafMalf: 'Absent', 
    leafMild: 'Absent', 
    leafShread: 'absent', 
    leafspotsHalo: 'absent', 
    leafspotSize: 'dna', 
    leafspotsMarg: 'dna', 
    leaves: 'Abnorm', 
    lodging: 'Yes', 
    moldGrowth: 'Absent', 
    mycelium: 'Absent', 
    plantGrowth: 'Abnorm', 
    plantStand: 'lt-normal', 
    precip: 'Normal', 
    roots: 'Norm', 
    sclerotia: 'Absent', 
    seed: 'Norm', 
    seedDiscolor: 'Absent', 
    seedSize: 'Norm', 
    seedTmt: 'none', 
    severity: 'pot-severe', 
    shriveling: 'Absent', 
    stem: 'Abnorm', 
    stemCankers: 'below-soil', 
    temp: 'norm', 
    caso: 1, 
    doenca: 'phytophthora-rot' } 
+0

你添加您的文章身體JSON? –

+0

我想你的'req.body'有一個'save'方法,如果沒有的話,你可以通過'caso = req.body;' –

+0

@BasimHennawi來覆蓋你的caso對象。 –

回答

3

您嘗試使用的save方法是模型的一部分,而不是您的req.body。所以,你必須擺脫線:

caso = req.body; 

因爲它覆蓋該行:

var caso = new Caso(); 
+0

現在我明白了。所以,我將不得不將每個值設置爲新的caso。喜歡,caso.doenca = req.body.doenca ...謝謝! –

+0

是的。如果有幫助請接受答案。 @GustavoMendonça –

+1

@GustavoMendonça或'var caso = new Caso(req.body)' – robertklep