如何从客户那里获取ID?(Express and Mongoose)

发布于 2025-02-11 02:08:29 字数 497 浏览 1 评论 0原文

我编写了一个代码来查找材料上的配方(这是我之前创建的对象),但我在代码中编写了对象ID。 我想让客户发送此ID。 如何以这种方式编写代码?


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);
    

});


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);
    

});


如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

≈。彩虹 2025-02-18 02:08:29

您可以在请求参数中从前端发送客户端ID。示例:http:yourdomain:端口/路线/:

后端中的食谱您可以这样处理:

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

    res.send(recipefound);
});

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);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文