I make a RestAPI for my diploma project and now I got a problem with one of my routes. I am sending a request with search params, but to me responding a route for finding by id. Maybe I just wrote incorrect request with params and my API thinks this is request with id?
My index.js
const express = require('express');
const mongoose = require('mongoose');
const cors = require('cors');
const cookieParser = require('cookie-parser');
const adminRoute = require('./routes/admin');
const courierRoute = require('./routes/couriers');
const customerRoute = require('./routes/customer');
const brandRoute = require('./routes/brands');
const adminTokenRoute = require('./routes/adminTokens');
const courierTokenRoute = require('./routes/courierTokens');
const dotenv = require('dotenv');
const app = express();
dotenv.config()
app.use(express.json({ limit: '30mb', extended: true }));
app.use(express.urlencoded({ limit: '30mb', extended: true }));
app.use(cookieParser());
/*app.use(cors({
credentials: true,
origin: 'http://localhost:3000'
}));*/
app.use(cors());
app.use(adminRoute);
app.use(courierRoute);
app.use(customerRoute);
app.use(brandRoute);
app.use(adminTokenRoute);
app.use(courierTokenRoute);
app.get('/', (req, res) => {
res.send('App is running');
})
const CONNECTION_URL = process.env.MONGODB_URL;
const PORT = process.env.PORT || 5001;
mongoose.connect(CONNECTION_URL, { useNewUrlParser: true, useUnifiedTopology: true,
useCreateIndex: true, useFindAndModify: false })
.then(() => app.listen(PORT, () => console.log(`Server Running on Port:
http://localhost:${PORT}`)))
.catch((error) => console.log(`${error} did not connect`));
mongoose.set('useFindAndModify', false);
My brands.js
const express = require('express');
const Brand = require('../models/catalog/brands');
const Admin = require('../models/users/admin');
const Manager = require('../models/users/manager');
const StoreKeeper = require('../models/users/storeKeeper');
const auth = require('../middleware/auth');
const router = express.Router();
router.get('/brands/:id', async (req, res) => {
const { id } = req.params;
try {
const item = await Brand.findById(id);
res.json({ item });
} catch (err) {
res.status(404).json({ error: 'Brand is not found!' });
}
});
router.get('/brands/search', async (req, res) => {
const { searchQuery } = req.query;
try {
const title = new RegExp(searchQuery, 'i');
const items = await Brand.find({ name: title });
res.json({ items });
} catch (err) {
res.status(404).json({ error: 'Wrong request' });
}
});
module.exports = router;
When I send request with id everything works well enter image description here
But when I send request with query params I got error response from route where I search item by id enter image description here
What it can be? Thanks in advance for all the answers.
CodePudding user response:
Place the Route /brands/search
above the Route /brands/:id
in './routes/brands/' file.
If you place Route /brands/:id
before Route /brands/search
, it will consider 'search' word as parameter 'id'