如何从客户那里获取ID?(Express and Mongoose)
我编写了一个代码来查找材料上的配方(这是我之前创建的对象),但我在代码中编写了对象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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在请求参数中从前端发送客户端ID。示例:http:yourdomain:端口/路线/:
后端中的食谱您可以这样处理:
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: