//backend
//api
export const getPostsBySearch=async (req,res)=>{
const {searchQuery , tags}=req.query;
try {
const title=new RegExp(searchQuery,'i');
const posts=await PostMessage.find({$or:[{title},{tags:{$in:tags.split(',') }}]})
//here we get tags as an array so finding the specific tag by spliting tags using array, because we get the array as an object here using ",". or = search by title or tags .RegExp = regex.
res.json({data:posts})
// i for ignoring case
} catch (error) {
}
}
//actions
export const getPostsBySearch=(searchQuery)=>async(dispatch)=>{
try {
const {data:{data}}=await api.fetchPostsBySearch(searchQuery);
console.log(data);
} catch (error) {
console.log(error);
}
}
//action in front end
export const getPostsBySearch=(searchQuery)=>async(dispatch)=>{
try {
const {data:{data}}=await api.fetchPostsBySearch(searchQuery);
console.log(data);
} catch (error) {
console.log(error);
}
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter