2015-03-12 4 views
-1

Я работаю с node.js, expressjs 4 и mongoose. Мой мангуст Шма является то, что:mongoose и node js insert/post данные

var MachineSchema = new mongoose.Schema({ 

serial_num: { type: String}, 
description: {type: String}, 
nature: {type: String, sparse: true}, 
mark: {type: String }, 
type: String, 
history: { 
     date: Date, 
     action: String, 
     description: String, 
     specif_infos: { 
      client: String, 
      number: Number 
     } 
    }, 
purchase_date: {type: Date}, 
actual_owner: { 
     first_name: {type: String}, 
     last_name: {type: String}, 
     phone: {type: Number}, 
     owner_address: 
      { 
       avenue: {type: String}, 
       city: {type: String}, 
       country: {type: String}, 
       postal_code: { type: Number} 
      } 
    }, 
actual_concess: { 
     name: String, 
     phone: Number, 
     concess_address: 
       { 
       avenue: String, 
       city: String, 
       country: String, 
       postal_code: { type: Number, min: 0, max: 99999} 
      } 
    } 
}); 

как я могу опубликовать некоторые данные? Я пытался использовать raw в POSTMAN, но не работает! любая идея пожалуйста?

и для моего контроллера: machine.js

exports.postMachines = function (req, res) { 

var machine = new Machine(); 

machine.serial_num = req.body.serial_num; 
machine.description = req.body.description; 
machine.nature = req.body.nature; 
machine.mark = req.body.mark; 
machine.type = req.body.type; 
machine.history = req.body.history; 
machine.purchase_date = req.body.purchase_date; 
machine.actual_owner = req.body.actual_owner; 
machine.actual_concess = req.body.actual_concess; 

machine.save(function (err) { 
    if (err) { 
     res.json({ message: 'la machine ne peut pas être ajoutée'});*/ 
     res.send(err); 
    } else { 
     res.json({ message: 'machine ajoutée', data: machine}); 
    } 
}); 

};

exports.getMachines = function (req, res) { 

    Machine.find(function (err, machines) { 
    if (err) { 
     res.send(err); 
    } 

    res.json(machines); 

}); 

}; exports.getMachine = функция (REQ, разреш) {

Machine.findById(req.params.id, function (err, machine) { 
    if (err) { 
     res.send(err); 
    } 
    res.json(machine); 
}); 

};

exports.getMachine = function (req, res) { 

Machine.findOne(req.params.mark, function (err, machine) { 
    if (err) { 
     res.send(err); 
    } 
    res.json(machine); 
}); 

};

exports.putMachine = function (req, res) { 

Machine.findById(req.params.id, function (err, machine) { 
    if (err) { 
     res.send(err); 
    } 

    machine.history = [req.body.history]; 
    machine.actual_owner = req.body.actual_owner; 
    machine.actual_concess = [req.body.actual_concess]; 

    return machine.save(function (err) { 
     if (!err) { 
      console.log("machine mise à jour"); 
     } else { 
      console.log("erreur : " + err); 
     } 
     return res.json(machine); 
    }); 
}); 

};

exports.deleteMachine = function (req, res) { 

Machine.findByIdAndRemove(req.params.id, function (err) { 
    if (err) { 
     res.send(err); 
    } 
    res.json({ message: 'machine supprimée!' }); 
}); 

};

exports.deleteMachines = функция (REQ, разреш) {

Machine.remove(function (err, machines) { 
    if (err) { 
     res.send(err); 
    } 

    res.json("machines supprimées"); 
}); 

};

+0

Не могли бы вы указать код конечной точки 'app.get ('/ blah') ....', чтобы мы могли видеть, что ожидает ваш контроллер. Просто опубликовать определение модели нехорошо. – freshnode

ответ

1

Вы должны сделать отображение между вашими функциями и метод HTTP, например:

var express = require('express'); 
var app = express(); 

app.get('/', function(req, res){ 
    res.send('hello world'); 
}); 

В вашем случае, вы shold передать объект приложения в файл маршрутизатора и сделать что-то вроде:

module.exports = function(app) { 

postMachines = function (req, res) { 

var machine = new Machine(); 

machine.serial_num = req.body.serial_num; 
machine.description = req.body.description; 
machine.nature = req.body.nature; 
machine.mark = req.body.mark; 
machine.type = req.body.type; 
machine.history = req.body.history; 
machine.purchase_date = req.body.purchase_date; 
machine.actual_owner = req.body.actual_owner; 
machine.actual_concess = req.body.actual_concess; 

machine.save(function (err) { 
    if (err) { 
     res.json({ message: 'la machine ne peut pas être ajoutée'});*/ 
     res.send(err); 
    } else { 
     res.json({ message: 'machine ajoutée', data: machine}); 
    } 
}); 
} 

app.get('/machines', getMachines); 
app.post('/machines', postMachines); 
app.delete('/machines/:id', deleteMachines); 
}