I need to create the following API end point using Next.js:
/api/products/[name]?keyword=${anykeyword}.
I know I need to create it inside pages/api/products/[name] directory in index.js. But how can I access keyword.
req.query only contains name.
following is my code:
import { connectToDatabase } from "../../../../util/mongodb";
import validate from "../../../../validation/product";
//capitalize first letter only to avoid errors
import capitalize from "lodash.capitalize";
export default async function productsHandler(req, res) {
const {
query: { name, keyword }, // here i cannot access keyword//
method,
} = req,
Name = capitalize(name),
{ db } = await connectToDatabase(),
searchCriteria = keyword ? keyword : "price";
switch (method) {
case "GET":
// @route GET api/products/[name]
// @desc get products of [name] from data base
// @access public
{
const products = await db
.collection("products")
.find({ category: Name })
.sort({ [searchCriteria]: 1 })
.toArray();
if (!products.length)
return res.status(404).json({ error: "no such category" });
res.status(200).json(products);
}
break;

req.queryshould return thekeywordbit according to github.com/vercel/next.js/issues/15472#issuecomment-678357324. Can you share your code?