0

I have my data like this in my database

[{
  "birth-date": "18/12/2010",
  "babies": [{
    "name": "James",
    "age": 8,
  }, {
    "name": "John",
    "age": 4,
  }]
}]

So I do my schema like below

var babiesSchema = new Schema({
    birth-date: Date, // tried String too
    babies: [{
        name: String,
        age: Number
    }]
});

var Babies = mongoose.model('babies', babiesSchema);

I do

Babies.find({}, function(response){
    res.json(response)
  })

I got null, is something wrong with my schema? There is no error in my node's terminal.

2 Answers 2

2

I would say that nothing is wrong with your schema. You just need to read documentation more carefully.

If you look documentation you will find that callback should have 2 parameters. First is for error and second is for result of search. So null means that there is no error. I would try to change code like this:

Babies.find({}, function(err, foundBabies){
    res.json(foundBabies)
})
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

Babies.find({}).exec(function (err, babies) {
        if (err) {
            return res.status(500).json({
                error: 'Cannot find Babies'
            });
        } else {
            res.json(babies);
        }
    });

Comments

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.