I cannot access the text that is inside an object array in Mongodb after applying foreach

Asked

Viewed 50 times

0

I am unable to access my array of objects that have been configured in my Schema. Schema using foreach.

const quizSchema = new mongoose.Schema(
    {
        question: String,
        answers : [
            {first: String},
            {second: String},
            {third: String}
        ]
    }
);

const Quiz = mongoose.model('Quiz', quizSchema);

When I do :

app.get('/nivelamento', (req, res) => {
    Quiz.find((err, allQuizzes) => {
        if (err){
            console.log(err);
        } else {                          
            const firstAnswer  = allQuizzes[1].answers[0].first;
            console.log(firstAnswer);

            res.render('nivelamento', {questions: allQuizzes});
        };
    });
});

Ok, my console returns what I want. But when I use foreach it returns me an array with _id plus the object. And when I try to access only the object, without the _id, I can’t.

<section class="nivelamento" id="nivelamento">
        <div class="container">
            <h2 class="nivelamento-titulo">Test your English!</h2>
           <% questions.forEach((quiz) =>{ %>
                <h4> <%= quiz.question %></h4>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
                        <label class="form-check-label" for="exampleRadios1">
                            <%= quiz.answers[0] %>
                        </label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
                        <label class="form-check-label" for="exampleRadios1">
                            <%= quiz.answers[1] %>
                        </label>
                    </div>
                    <div class="form-check">
                        <input class="form-check-input" type="radio" name="exampleRadios" id="exampleRadios1" value="option1" checked>
                        <label class="form-check-label" for="exampleRadios1">
                            <%= quiz.answers[2] %>
                        </label>
                   </div>              
            <% }); %>                                 
        </div>
    </section>

I have tried : quiz.answers.first (but it says 'first' is 'Undefined'); I already tried: quiz.Answers[0]. first (get '[0]' is also 'Undefined');

So at the moment I can only print on the screen:

inserir a descrição da imagem aqui

Can anyone tell me why I can’t access the answer text and why I have 3 chekbox created with no values?

  • But the way to access that information should be quiz.answers[0].first even, unless this information is still serialized in string form for some reason, in which case it would be JSON.parse(quiz.answers[0]).first. Anyway, post your HTML in the question, not an image with part of the cropped code.

  • It doesn’t work to make JSON.parse(quiz.Answers[0]).first. I also took the images and put the code.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.