Node.js 快速服务器获取请求用户ID
我想为taskuser/getallusers?userId=''创建一个GET API路由 我想获取分配给特定用户的所有任务。但是当我测试这个调用()但没有错误时 我仍然可以将所有任务分配给任何用户。有人能告诉我出了什么问题吗 模型任务用户:Node.js 快速服务器获取请求用户ID,node.js,mongodb,express,mongoose,Node.js,Mongodb,Express,Mongoose,我想为taskuser/getallusers?userId=''创建一个GET API路由 我想获取分配给特定用户的所有任务。但是当我测试这个调用()但没有错误时 我仍然可以将所有任务分配给任何用户。有人能告诉我出了什么问题吗 模型任务用户: const mongoose = require('mongoose'); const TaskuserSchema = new mongoose.Schema({ task_name:{ type: String, required:
const mongoose = require('mongoose');
const TaskuserSchema = new mongoose.Schema({
task_name:{
type: String,
required: true,
minlength: 1,
unique: true,
},
userId: {
type: mongoose.Schema.Types.ObjectId,
required: true,
},
task_category: String,
task_xpreward: Number,
task_completed: Boolean,
task_difficulty: Number,
task_city : String,
});
Api路线:
router.get('/getalltasks/:userid', cors(),async(req,res) => { // Add /:userid
var userid = req.params.userid;
Taskuser.find({ userId: userid}, function(err, tasks) {
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
res.send(err);
res.json(tasks); // return all tasks that are in JSON format
});
});
编辑:新建api路线您应将代码更新为:
router.get('/getalltasks/:userid', cors(),async(req,res) => { // Add /:userid
var userid = req.params.userid;
Taskuser.find({ userId: userid}, function(err, tasks) {
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err)
res.send(err);
res.json(tasks); // return all tasks that are in JSON format
});
});
文档:将api路由文件更改为:
router.get("/getalltasks", cors(), async (req, res) => {
var userid = req.params.userId;
Taskuser.find({ userId: userid }, function(err, tasks) {
var userid = req.params.userId;
// if there is an error retrieving, send the error.
// nothing after res.send(err) will execute
if (err) res.send(err);
res.json(tasks); // return all tasks that are in JSON format
});
});
Taskuser.find({userId:req.params.userId},func…如果我实现了你的解决方案,我会得到一个空的json正文。你确定有任务的userId与你在
get
parameters中传递的相同吗?以下是带有我的Taskuser在mongodb中的表的图像和邮递员信息()我将调用从:==>更改为:==>但仍然没有更改更新了我的代码(将var userid=req.params.userid;
更改为var userid=req.params.user\u id;
),因为您似乎正在postmanempty json正文中使用user\u id