我的 express 后端出现了这个奇怪的错误,在访问路径中包含的参数时,我从 Firefox 和移动浏览器收到错误。当我从 Chrome 发送请求时,它工作正常。虽然我知道如何绕过它,但我真的很好奇什么实际上不起作用。
例如,我在这条路径上遇到了很多麻烦:
router.route('/:id/:restaurantId/addRequest').post((req, res) => {
const name = req.body.name;
const newRequest = new Request({ name });
Organization.findById(req.params.id)
.then(org => {
const i = org.restaurants.findIndex(rest => rest._id == req.params.restaurantId);
if (org.restaurants[i].requests.findIndex(req => req.name == name) === -1) {
org.restaurants[i].requests.push(newRequest);
org.save()
.then(() => res.json('Request Added.'))
.catch(err => res.status(400).json('Error: ' + err));
} else {
res.json('Request Already Exists');
}
})
});
但是当我尝试修改路径以便我只在我发送的请求 JSON 对象中发送我需要的参数时,它工作正常。
router.route('/addRequest').post((req, res) => {
const name = req.body.name;
const newRequest = new Request({ name });
Organization.findById(req.body.orgID)
.then(org => {
const i = org.restaurants.findIndex(rest => rest._id == req.body.restID);
if (org.restaurants[i].requests.findIndex(req => req.name == name) === -1) {
org.restaurants[i].requests.push(newRequest);
org.save()
.then(() => res.json('Request Added.'))
.catch(err => res.status(400).json('Error: ' + err));
} else {
res.json('Request Already Exists');
}
})
});
这实际上可能不是根本问题,但是,根据我测试过的所有内容,只有当我有某种路由参数时,路由才会中断。如果有人能给我一些清晰的信息,那就太棒了。
以下是我遇到问题的文件中的所有路由:
router.route('/:id').get((req, res) => {
Organization.findById(req.params.id)
.then(Organizations => res.json(Organizations))
.catch(err => res.status(400).json('Error: ' + err));
});
router.route('/add').post((req, res) => {
const id = req.body.id;
const name = req.body.name;
const restaurants = [];
const newOrganization = new Organization({ name, restaurants });
newOrganization._id = id;
newOrganization.save()
.then(() => res.json('Organization added!'))
.catch(err => res.status(400).json('Error: ' + err));
});
router.route('/:id/addRestaurant').post((req, res) => {
const name = req.body.name;
const requests = [];
const tables = [];
const newRestaurant = new Restaurant({ name, requests, tables });
Organization.findById(req.params.id)
.then(org => {
org.restaurants.push(newRestaurant);
org.save()
.then(() => res.json('Restaurant Added.'))
.catch(err => res.status(400).json('Error: ' + err));
})
});
router.route('/:id/:restaurantId/addTable').post((req, res) => {
const name = req.body.name;
const code = req.body.code;
const newTable = new Table({ name, code });
Organization.findById(req.params.id)
.then(org => {
const i = org.restaurants.findIndex(rest => rest._id == req.params.restaurantId);
org.restaurants[i].tables.push(newTable);
org.save()
.then(() => res.json('Table Added.'))
.catch(err => res.status(400).json('Error: ' + err));
})
});
router.route('/:id/:restaurantId/addRequest').post((req, res) => {
const name = req.body.name;
const newRequest = new Request({ name });
Organization.findById(req.params.id)
.then(org => {
const i = org.restaurants.findIndex(rest => rest._id == req.params.restaurantId);
if (org.restaurants[i].requests.findIndex(req => req.name == name) === -1) {
org.restaurants[i].requests.push(newRequest);
org.save()
.then(() => res.json('Request Added.'))
.catch(err => res.status(400).json('Error: ' + err));
} else {
res.json('Request Already Exists');
}
})
});
router.route('/:id/:restaurantId/getTables').get((req, res) => {
Organization.findById(req.params.id)
.then(org => {
const i = org.restaurants.findIndex(rest => rest._id == req.params.restaurantId);
res.json(org.restaurants[i].tables);
})
})
router.route('/:id/:restaurantId/getRequests').get((req, res) => {
Organization.findById(req.params.id)
.then(org => {
const i = org.restaurants.findIndex(rest => rest._id == req.params.restaurantId);
res.json(org.restaurants[i].requests);
})
})
router.route('/:id/:restaurantId/deleteTable').post((req, res) => {
Organization.findById(req.params.id)
.then(org => {
const i = org.restaurants.findIndex(rest => rest._id == req.params.restaurantId);
org.restaurants[i].tables = org.restaurants[i].tables.filter(table => table.code !== req.body.code)
org.save()
.then(() => res.json('Table Deleted.'))
.catch(err => res.status(400).json('Error: ' + err));
})
.catch(err => res.status(400).json('Error: ' + err));
})
router.route('/:id/:restaurantId/deleteRequest').post((req, res) => {
Organization.findById(req.params.id)
.then(org => {
console.log(req.body.id);
const i = org.restaurants.findIndex(rest => rest._id == req.params.restaurantId);
org.restaurants[i].requests = org.restaurants[i].requests.filter(request => request._id != req.body.id)
org.save()
.then(() => res.json('Request Deleted.'))
.catch(err => res.status(400).json('Error: ' + err));
})
.catch(err => res.status(400).json('Error: ' + err));
})
router.route('/:id/:restaurantId/updateCode').post((req, res) => {
Organization.findById(req.params.id)
.then(org => {
const i = org.restaurants.findIndex(rest => rest._id == req.params.restaurantId);
org.restaurants[i].tables.forEach(table => {
if (req.body.oldCode === table.code)
table.code = req.body.newCode;
});
org.save()
.then(() => res.json('Code Updated.'))
.catch(err => res.status(400).json('Error: ' + err));
})
.catch(err => res.status(400).json('Error: ' + err));
})