Delete productId with Nodejs & Mongodb

1

Good! I am developing a mini API RESTFul with Node.js and Mongodb . The question is this, I am developing 4 basic functions for my API that would be GET, POST, UPDATE, DELETE .

I'm having an error when performing DELETE and it's the following error:

Cannot DELETE /api/product/58feb76ed70f040be8d69e6d

And this would be my code:

'use strict'

const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');

const Product = require('./modelos/product');

const app = express();
const port = process.env.PORT || 3001;

app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());

app.get('/api/product', (req, res) => {
  Product.find({}, (err, products) => {
    if(err) return res.status(500).send({message: 'Error al realizar peticion ${error}'});
    if(!products) return res.status(404).send({message: 'No existen productos'});

    res.send(200, {products});
  });
});

app.get('/api/product/:productId', (req, res) => {
  let productId = req.params.productId;

  Product.findById(productId, (error, product) => {
    if (error) return res.status(500).send({message: 'Erro al realizar peticion ${error}'});
    if(!product) return res.status(404).send({message: 'El producto no existe!'});

    res.status(200).send({product});
  });
});

app.post('/api/product', (req, res) => {
  console.log('POST /api/product');
  console.log(req.body);

  let product = new Product()
  product.name = req.body.name
  product.picture = req.body.picture
  product.price = req.body.price
  product.category = req.body.category
  product.description = req.body.description

  product.save((err, productStored) =>{
    if (err)res.status(500).send({message: 'Error al salvar en la base de datos:  ${err}'});

    res.status(200).send({product: productStored});
  });
});

app.put('/api/product:productId', (req, res) => {

});

app.delete('/api/product:productId', (req, res) => {
  let productId = req.params.productId;

  Produc.findById(productId, (err, product) =>{
    if(err) res.status(500).send({message: 'Error al borrar el producto ${err}'});

    product.remove(err =>{
      if(err) res.status(500).send({message: 'Error al borrar el producto ${err}'});
      res.status(200).send({message: 'El producto ha sido eliminado'});
    })
  })
});

mongoose.connect('mongodb://localhost:27017/shop', (error, res) =>{
  if(error){
    return console.log('Error al conectar a la base de datos: ${error}' );
  }
  console.log("Conexion a la base de datos correcta");

  app.listen(port, () => {
    console.log('API REST corriendo en http://localhost:${port}');
  });
});

I am uploading the products to the test API with POSTMAN .

    
asked by David Ramirez 25.04.2017 в 17:32
source

1 answer

1

Problem:

You are missing a bar ( / ) to separate the :productId parameter in the /api/product route.

Solution:

app.delete('/api/product/:productId', (req, res) => {

PD: The same error is in app.put .

    
answered by 25.04.2017 / 18:23
source