Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
137
rated 0 times [  139] [ 2]  / answers: 1 / hits: 5802  / 4 Years ago, sat, april 4, 2020, 12:00:00

I'm trying to get a user profile from a database and return it as a json object when the profile url (localhost:3000/Profile/1) is ran. but i am getting this error: TypeError: Cannot destructure property id of req.params as it is undefined.



here is the code in the express server.



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])})
})



i used postman to send the get request.


More From » node.js

 Answers
27

You passing the wrong parameters to the get function


E.g.


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

In your case


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

[#4268] Wednesday, April 1, 2020, 4 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
turnerf

Total Points: 620
Total Questions: 101
Total Answers: 109

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
turnerf questions
Thu, Oct 7, 21, 00:00, 3 Years ago
Sat, Mar 20, 21, 00:00, 3 Years ago
Tue, Mar 26, 19, 00:00, 5 Years ago
;