0

I'm trying to do an API call to my express server to fetch employees that work in the same place based on the location ID. However, the API call returns just an empty array while it does work in the command-line interface.

Employee model

module.exports = mongoose => {
  var schema = mongoose.Schema(
    {
      first_name: String,
      last_name: String,
      address: {
        housenumber: Number,
        street: String,
        city: String,
        zip: Number,
        country: String
      },
      phone: Number,
      mobile: Number,
      email: String,
      enrollment_date: Date,
      staff_id: Number,
      location: { type : mongoose.Schema.ObjectId, ref : 'location' },
      department: String,
      function: String, 
      active: Boolean
    },
    { timestamps: true }
  );

  schema.method("toJSON", function() {
    const { __v, _id, ...object } = this.toObject();
    object.id = _id;
    return object;
  });

  const Employee = mongoose.model("employee", schema);
  return Employee;
};

Employee routing for API

  router.get("/location/:location_id", employees.findAllByLocation);

Employee controller handling above call

exports.findAllByLocation = (req, res) => {
  Employee.find({ location: req.params.location_id })
    .then(data => {
      res.send(data);
    })
    .catch(err => {
      res.status(500).send({
        message:
          err.message || "Some error occurred while retrieving Employees."
      });
    });
};

Dummy database data to test on

Dummy database data to test on

Postman API call result

Postman API call result

However, trying to find the user with that location ID in the command line interface does work and gives the desired output.

[Powershell output3

So somehow it messes up and I can't seem to figure out why it's doing this. I did some research and found that it might have to do with the location being a reference as an ObjectId. So I tried wrapping the req.params.location_id to an ObjectId might fix it but that didn't work.

What's the best way to get this working?

5
  • What is app.use() for that router? Commented Apr 10, 2020 at 9:03
  • app.use("/api/employees", router); The method kinda works tho, if I leave out the condition to find by location, it shows a list off all employees. It's just somehow that it doesnt properly process the location id. Commented Apr 10, 2020 at 9:06
  • @m1ch14l You have defined location to be an ObjectId at the schema level but clearly it is stored as String in the database (from the screenshot)? Try changing the database entry with location to correct ObjectId and then test it out. Commented Apr 10, 2020 at 15:14
  • 1
    @ambianBeing I feel stupid now, I changed it in the database and now it's working perfectly. Thanks so much! Commented Apr 10, 2020 at 19:49
  • @m1ch14l Most welcome. It happens all the time to us devs 👍 Commented Apr 10, 2020 at 19:50

1 Answer 1

1

In order to use promise chain, you have to return something and then returned value will be passed chained “then()” as data. In your example you should

      return Employee.find({location:req.params.location_id})
Sign up to request clarification or add additional context in comments.

2 Comments

The output is still just []
can you console.log(req.params.location_id) in controller

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.