[Fixed] Not able to fetch data from database while giving input to mongoose query through a href tag in Node js

Issue

This is my Schema

const AutomationSchema=new mongoose.Schema(
  {EventName:String,
    EventDate:String,
     EventLocation:String,
     EventDetails:String
  }
)

events model

const EventsModel=new mongoose.model("Events",AutomationSchema)

This is my express route
Although it is getting the right route param value in variable "eventName" and the record of "eventName" also exists in database but it is returning me a "null" in the findOne query ,But when I tried to put a string like {EventName:"Sports"} e.g then it fetches the record correctly, Iam not able to figure out where did i do something wrong??

app.get("/events/:EventName",function(req,res)
{ var eventName=req.params.EventName

  EventsModel.findOne({EventName:eventName},function(err,data)
  {
console.log(data)

  }

)})

Solution

There is a chance for this to happen if there is whitespace on any side of the event name. Try removing the whitespace using the trim method before passing it to the query

var eventName=req.params.EventName.trim()

Leave a Reply

(*) Required, Your email will not be published