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

Time:06-27

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 Admin = require('../middleware/admin')
const Auth = require('../middleware/auth')
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);
    

});

module.exports = router;


CodePudding user response:

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