Adding elements to a mongodb database

0

With a problem, I'm trying to insert a new record in my database, where the entry is a {url: "example.com } I'm using the express framework, the mongodb database and the mongoose library (I've been able to do other things, add an id, delete an id, access a url linked to an id, etc, only that part I'm not able to do) / p>

Below is the code that will make the change in the database:

const mongoose = require('mongoose');
const Chaordic = mongoose.model('Chaordic');

// Add a new URL 
exports.addUrl = (req,res,next) => {

var chaordic = new Chaordic();
chaordic.id = req.params.id;
chaordic.url = req.body.url;
chaordic.hits = 0;
chaordic.shortUrl = shortenUrl(chaordic.url)
chaordic.save().then(x =>{
    res.status(201).send({
        id: chaordic.id
    });
}).catch(e =>{
    res.status(204).send(e);
})
}

Here is the part of the code that I request to enter the data:

const express = require('express');
const router  = express.Router();
const controller = require('../controllers/controller');

router.post('/users/:id/urls', controller.addUrl);

module.exports = router;

And here is my model

const mongoose = require('mongoose')
const Schema = mongoose.Schema;
const schema = new Schema({
id: {
    type:String,
    unique: true

},
url: {
    type: String
},
hits: {
    type: Number
},
shortUrl: {
    type: String
}
});

module.exports = mongoose.model('Chaordic', schema); 

When I give request to the server my return is error 204 (Error that I placed to be returned if it did not send the data) can anyone help me?

    
asked by anonymous 15.04.2018 / 05:08

0 answers