How to get id from client?(express and mongoose)

Viewed 46

I wrote a code to find the recipes base on the materials(this a object that i created before) but I wrote the object id in the code. I want client send me this id. how can i write my code in this way?


const express = require('express');
const { Recipe, validate } = require('../models/recipes');
const router = express.Router();


router.get('/',async(req,res)=>{
    const recipefound=await Recipe.find({materials:'62b703256f9ea31a51adde62'})//The object id
                                                 // ^^^^^^^^^^^^^^^^^^^^^^^^

    res.send(recipefound);
    

});


1 Answers

You can send the client ID from front end in request params. Example: http:yourdomain:port/route/:recipeID

In backend you can handle like this:

router.get('/',async(req,res)=>{
    const recipefound=await Recipe.find({materials: req.params.recipeID})

    res.send(recipefound);
});
Related