Octo
Octo

Reputation: 103

Mongoose query returns empty

I am trying to create a Node.js API endpoint to return some values for population of a select control.

I have the following document in a collection called projectStatusValues...

{
  "_id": {
    "$oid": "5cab4b2b38802527df2f7ab2"
  },
  "projectStatusDesc": [
    "Complete",
    "Pre-Start",
    "Active"
  ]
}

And my Mongoose model is defined as...

const projStatusModel = mongoose.model("projectStatusValues", {
    "projectStatusDesc": [
        String
    ]
});

The finally, I'm using this code to retrieve the array values...

app.get('/v1/projStatus', async (request, response) => {
    try {
        var status = await projStatusModel.find().exec();
        response.send(status);
    } catch (error) {
        response.status(500).send(error);
    }
});

The endpoint looks good and I get a 200 response but an empty string is returned. Any ideas?

Thanks!

Upvotes: 1

Views: 383

Answers (2)

Octo
Octo

Reputation: 103

Renamed the collection from projectStatusValues to projectstatusvalues and this worked.

Upvotes: 0

Enslev
Enslev

Reputation: 622

You are missing Schema

const Schema = mongoose.Schema;
const projStatusModel = mongoose.model("projectStatusValues", new Schema ({
    "projectStatusDesc": [
        String
    ]
}));

Upvotes: 1

Related Questions