保存不是一个功能

时间:2016-11-14 19:12:14

标签: node.js mongoose

我需要插入一个' caso'在我的数据库中,但我收到一条错误,说caso.save is not a function。我已经尝试了数千种不同的插入方式,但没有一种方法可行。我做错了吗?

PS:我的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' }

1 个答案:

答案 0 :(得分:3)

您尝试使用的save方法是模型的一部分,而不是您的req.body。所以你必须摆脱那条线:

caso = req.body;

因为它超越了这一行:

var caso = new Caso();