Sunday, 4 June 2017

Probleme during the POST nodejs express and mongoose

I have a probleme during a POST on my API RESTful with express and mongoose as orm. Basically I've created a route to add a new element in my mongo database.

This is the following route :

app.post('/crime/add', function (req, res) {
let new_crime = new Crime();

new_crime.description = req.body.description;
new_crime.nature = req.body.nature;

new_crime.save(function(error){
    if (error){
        res.json(error);
        return(console.log(error));
    }
    res.json({message:"Success !"});
})});

This is the schema of my database :

var express  = require('express');
var mongoose = require('mongoose');
var Schema   = mongoose.Schema;
var crimeSchema     = new Schema({
  id:          Number,
  nature:      String,
  incident:    String,
  crimecode:   String,
  discrict:    String,
  report:      Number,
  date:        Date,
  weapon:      String,
  shooting:    Boolean,
  domestic:    Boolean,
  shift:       String,
  year:        Number,
  month:       Number,
  day:         String,
  ucrpart:     String,
  x:           Number,
  y:           Number,
  streetname:  String,
  xstreetname: String,
  location:    Number
},
{
  collection: 'mean'
});

module.exports = mongoose.model('Crime', crimeSchema);

When I execute this following code in postman I have the message "Success !" but when I want to GET this data I can't found it.

I don't know if it's because I don't have all the columns in my routes (I have only description and naturecode for example just to try to add something in the database).

If someone can explain me where and why my code didnt work Thank you very much



via Nassim El Hormi

No comments:

Post a Comment