简体   繁体   中英

Why do I get the PUT request with POSTMAN not done?

Before embarking on the routes of my application, I have created some requests with POSTMAN of which PUT is not made to me in full.

This is my configuration of my server in ExpressJS:

const express = require('express');
const morgan = require('morgan');
const helmet = require('helmet');
const mongoose = require('mongoose');

const app = express();

// Settings
app.set('port', process.env.PORT || 3000);
mongoose.connect('mongodb://localhost/mevn-curso', { 
    useNewUrlParser: true,
    useFindAndModify: false,
    useCreateIndex: true
})
        .then(db => console.log('DB is connected'))
        .catch(err => console.log(err));

mongoose.set('useNewUrlParser', true);
mongoose.set('useFindAndModify', false);
mongoose.set('useCreateIndex', true);

// Middlewares
app.use(morgan('dev'));
app.use(express.json());
app.use(helmet());

// Routes
app.use('/tasks/', require('./routes/tasks'));


// Static files
app.use(express.static(__dirname + '/public'))

app.listen(app.get('port'), ()=> {
    console.log('Server on port', app.get('port'));
});

It works for me normally and this is the router I am using, which is inside the tasks.js file in the routes folder:

const express = require('express');
const router = express.Router();

const Task = require('../models/Task');


router.get('/', async (req,res)=> {
  const tasks = await Task.find();
  res.json(tasks);
})

router.post('/', async (req,res) => {
   const task = new Task(req.body);
   await task.save();
   res.json({
       status: "Task Saved"
   })
})

router.put('/:id', async (req,res)=> {
    console.log(req.params._id);
    console.log(req.body);
    await Task.findByIdAndUpdate(req.params._id, req.body)
    res.json('recivied');
    console.log('Listo')
})

module.exports = router;

In console does not seem to give me any error. I make the request with normal POSTMAN, and it returns the logs of the console. Even the server answers the json and everything. But the data in the database is not changed. This does not happen with GET or POST, on the contrary, everything is going well.

Here I leave you how I make the request with POSTMAN. First of all I am going to show you the data that I already have in the database, with the get request that is normally done with the browser:

带有GET请求的数据

ready, when I'm making the PUT request this is my configuration in POSTMAN:

邮递员配置1

It's a json type content type because that's what I'm going to process, then comes the body:

邮递员配置2

and this is the answer in the console:

安慰

What do you think it could be?

as I see the console.log of req.params._id is undefined: change the

Task.findByIdAndUpdate(req.params.id, req.body) changed _id to id

router.put('/:id', async (req,res)=> {
   console.log(req.params.id);
   console.log(req.body);
   await Task.findByIdAndUpdate(req.params.id, req.body)
   res.json('recieved');

})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM