expressJS is preventing me to post a resource

Issue

I’m trying to build a mini app in express, the "database" I’m using is a local array object file, I can retrieve resources from this "database" but for some reason I’m not able to post (push) a new object to this object array. This is how the code looks like:

server.js:

const express = require('express');
const app = express();
const userRouter = require('./routes/user.js');

const port = process.env.PORT || 3000;

app.use(express.json());
app.use(express.text());

app.use('/user', userRouter);
app.listen(3000, () => console.log(`listening at ${port}`));

user.js:

const express = require('express');
const BBDD = require('./BBDD.js');
const userRouter = express.Router();

userRouter.get('/:guid', (req, res, next) => {
    const { guid } = req.params;
    const user = BBDD.find(user => user.guid === guid);
    if (!user) res.status(404).send()
    res.send(user);
    next();
});

userRouter.post('/', (req, res, next) => {
    let user = {};
    user.name = req.body.name;
    user.id = req.body.id;
    BBDD.push(user);
    next();
});

module.exports = userRouter;

And this is my local "database" file I want to perform logical CRUD operations:

BBDD.js

const BBDD = [{

            index: 0,
            guid: "1",
            name: "Goku"
        },
        {
            index: 1,
            guid: "2",
            name: "Vegeta"
        },
    
    ];
    
    module.exports = BBDD;

this is how I try to post a new resource, and this is the error I get:

enter image description here

It seems to be in order, but it won’t work and can’t find the bug.

Solution

Remove the next and send a response .express is having trouble finding the next matching handler because there is none

Answered By – Asad Awadia

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published