0

We are trying to figure out how get the data from the database but the result get "null"

the model

const mongoose = require("mongoose");

const ClubSchema = new mongoose.Schema({
  nomClub: String,
  classement: String,
  dateMatch: String,
  classementDB: String,
  logo: String,
  adversaire: String,
});
const SportSchema = new mongoose.Schema({
  nom: String,
  clubs: [ClubSchema],
});
module.exports = mongoose.model("sport", SportSchema);

and the back

getSportAdversaire: (req, res) => {
    Sport.findOne({ "clubs.nomClub": "Stade Rennais" }, (err, data) => {
      if (err) {
        console.log(err);
        res.json({ message: "une erreur s'est produite" });
      } else {
        res.json(data);
        console.log(data);
      }
    });
  },

1 Answer 1

1

You dont need to use the key as clubs.nomClub. You can simply use the key as nomClub. Your key name is nomClub and not clubs.nomClub.

sport.findOne({ "nomClub": "Stade Rennais" }, (err, data) => {
  if (err) {
    console.log(err);
    res.json({ message: "une erreur s'est produite" });
  } else {
    res.json(data);
    console.log(data);
  }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Same problem, same answer "null" :(
@Wendy it seems that there is a typo in your Collection name. The first letter S is small cased in defenition where as capital cased while finding from the collection

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.