0

I have 2 collections like this:

const vocaSchema = {
    word: String,
    type: String,
    meaning: String,
    sente: String,
    semean: String,
    sug: String
};

const Voca = mongoose.model('Voca', vocaSchema);

const setVocaSchema = {
    lessonId: String,
    vocaList: [vocaSchema]
};

const SetVoca = mongoose.model('SetVoca', setVocaSchema);

and now I want to push a new Voca to vocaList field, and this is my code:

app.post('/lesson/add-voca/:lessonID', (req, res) => {
    const lessonID = req.params.lessonID;

    const newVoca = new Voca({
        word: req.body.addWord,
        type: req.body.addType,
        meaning: req.body.addMeaning,
        sente: req.body.addStc,
        semean: req.body.addStcm,
        sug: req.body.addSggt
    });

    console.log(newVoca);

    SetVoca.findOneAndUpdate({ lessonId: lessonID }, { $push: { vocaList: newVoca } }, (err, result) => {
        if (!err) {
            console.log(result);
            res.redirect('/lesson/add-voca/' + lessonID);
        } else {
            console.log(result);
            res.render('error');
        }
    });
});

and the result is undefined :( enter image description here
I don't know how to fix it. Anyone, please help me, thank you so much!

1 Answer 1

1

You need to push an object not a mongoose model.

Also you need to create a mongoose schema using mongoose.schema

const mongoose = require("mongoose");

const vocaSchema = new mongoose.Schema({
  word: String,
  type: String,
  meaning: String,
  sente: String,
  semean: String,
  sug: String
});

//const Voca = mongoose.model("Voca", vocaSchema);

const setVocaSchema = new mongoose.Schema({
  lessonId: String,
  vocaList: [vocaSchema]
});

const SetVoca = mongoose.model("SetVoca", setVocaSchema);

module.exports = SetVoca;

And in your post route:

app.post("/lesson/add-voca/:lessonID", (req, res) => {
  const lessonID = req.params.lessonID;

  const newVoca = {
    word: req.body.addWord,
    type: req.body.addType,
    meaning: req.body.addMeaning,
    sente: req.body.addStc,
    semean: req.body.addStcm,
    sug: req.body.addSggt
  };

  console.log(newVoca);

  SetVoca.findOneAndUpdate(
    { lessonId: lessonID },
    { $push: { vocaList: newVoca } },
    { new: true },
    (err, result) => {
      if (!err) {
        console.log(result);
        res.redirect("/lesson/add-voca/" + lessonID);
      } else {
        console.log(err);
        res.render("error");
      }
    }
  );
});

Test:

Let's say we have this document with an empty vocaList array:

{
    "_id": "5e61f34358a41b7ee0e6fa40",
    "lessonId": "lesson1",
    "vocaList": [],
    "__v": 0
}

When we send a POST request with this url ../lesson/add-voca/lesson1, and with this request body:

    "addWord": "word1",
    "addType": "type1",
    "addMeaning": "meaning",
    "addStc": "sente",
    "addStcm": "semean",
    "addSggt": "sug"
}

The result will be like this:

{
    "_id" : ObjectId("5e61f34358a41b7ee0e6fa40"),
    "lessonId" : "lesson1",
    "vocaList" : [
        {
            "_id" : ObjectId("5e61f5900b73daa104e2ee75"),
            "word" : "word1",
            "type" : "type1",
            "meaning" : "meaning",
            "sente" : "sente",
            "semean" : "semean",
            "sug" : "sug"
        }
    ],
    "__v" : 0
}
Sign up to request clarification or add additional context in comments.

6 Comments

@ManhCuong I just updated answer, I made modifications to your schema also. Can you try like that?
I just edited my code like you, but the result is null
@ManhCuong can you check your collection if it is updated?
I checked that, it doesn't even create the setvoca collection, I make sure I cleaned the database before going to build with your code.:(
@ManhCuong ok I will try this
|

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.