0

I'm writing a simple RESTful API using mongoose and their CRUD operations.

It "works", when i fire a post request (the only type of request for now) to the server it returns me an error 500 (as it should be) but the error object are empty and the server logger returns me an OK 200 state:

POST /omi/v1/clientes 200 17.586 ms - 25 // server: all is cool!


{
    "status": 500,
    "error": {}
} // response: nope, nothing is cool, baby, but i don't tell you why, or what.

I never seen this behavior before (i admit it, i'm noob) but reading my code i can't find the problem... here it is:

clientes.js

"use strict";

// instancias de modelos
var Cliente = require('../models/models').Cliente;
// var reqHelper = require('./util');

var clientes = {

  crear: function (req, res) {

    var nuevoCliente = {
      _id: req.body._id,
      nombre: req.body.nombre,
      direccion: req.body.direccion,
      telefono: req.body.telefono
    };

    Cliente.create(nuevoCliente, function (err, cliente) {
      if (err) return res.json({status: 500, error: err});
      res.json({ status: 200, cliente: cliente });
    }); // fin Cliente.create
  }, // fin crearCliente

  actualizar: function(req, res) {
    var ci = req.body._id;

    var clienteData = {};
    // reqHelper(req.body, clienteData);

    Cliente.update({ _id: ci }, clienteData, function(err, cliente) {
      if (err) return res.json({status: 500, error: err});
      res.json({ status: 200, cliente: cliente });
    }); // fin Cliente.update
  }, //fin actualizarCliente

  borrar: function(req, res) {
    Cliente.remove({ _id: req.body._id }, function(err) {
      if (err) return res.json({status: 500, error: err});
      res.json({ status: 200, msg: 'Cliente borrado' });
    });
  }, // fin borrarCliente
}; // fin actions

module.exports = clientes;

index.js

"use strict";

var express = require('express'),
    router  = express.Router();

var clientes = require('./clientes');
var items    = require('./items');

router.route('/clientes')
  .post(clientes.crear)
  .put(clientes.actualizar)
  .delete(clientes.borrar);

router.route('./items')
  .post(items.crear)
  .put(items.actualizar)
  .delete(items.borrar);

module.exports = router;

And this is it... thanks for any help/idea

Nano
  • 795
  • 3
  • 15
  • 36
  • 2
    The first thing: instead of line "if (err) return res.json({status: 500, error: err});" do simple "console.log(err)", and say if you will see the error IN CONSOLE. And the second thing: put in console log next thing: "req.body" to see what parameters you are getting from the client and check if they appropriate to your mongoose model. – Simcha Dec 17 '14 at 14:57
  • @Simha I was sending the data as form-data and not as x-www-urlencoded ... But I have a question: What is the difference between both? Do you have some documentation that i can read about it? Thank you very much for your answer! – Nano Dec 17 '14 at 15:13
  • Here 2 links that can be useful for you: http://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data and http://www.restfm.com/restfm-manual/web-api-reference-documentation/submitting-data/applicationx-www-form-urlencoded-and . This is the first thing, and the second in any case check what are you getting from client(do things that I told in first comment), and say: what you see in console? – Simcha Dec 18 '14 at 07:57

0 Answers0