TypeError: Cannot destructure property id of req.params as it is undefined

TypeError: Cannot destructure property id of req.params as it is undefined

我正在尝试从数据库中获取用户配置文件并 return 它作为一个 json 对象,当配置文件 url (localhost:3000/Profile/1 ) 是 运行。但我收到此错误: TypeError: Cannot destructure 属性 id of req.params 因为它未定义。

这是快递服务器中的代码。

const express = require('express');
const bodyParser = require('body-parser');
const bcrypt = require('bcryptjs');
const cors = require('cors');
const knex = require('knex');

const app = express();
app.use(cors());
app.use(bodyParser.json());


app.get('/Profile/:id', (res,req) =>{
    const {id} = req.params;
    db.select('*').from('user').where({id})
    .then(user => {
    res.json(user[0])})
})

我使用邮递员发送获取请求。

参数的顺序是Request, Response所以你必须这样做:

app.get('/profile/:id', (request, response) => {
    const { id } = request.params;
    db.select('*').from('user').where({id}).then(
       user => {
           res.json(user[0])
       });
});

我看到你的错误了,你输入了函数 (res, res) 并且应该是 (req,res)。

您向 get 函数传递了错误的参数

例如

// respond with "hello world" when a GET request is made to the homepage
app.get('/', function (req, res) {
  res.send('hello world')
})

你的情况

app.get('/Profile/:id', (req, res) =>{
   console.log(req.params)
}