0

I am getting a collection of a data, which is questions from mongoDB, and I putting that in a set of random question. The set is not taking the input, and it is returning empty.

 module.exports.java = async (req, resp) => {
// const choice = req.body;
let quest = [];
let option = [];
let sort =[];
try {
    let data = await Questions.find({ type: 'javascript'})
    //for creating an array of questions
    data.forEach((elements) => {
        //Stores all the string in an array
        quest.push(elements.description);
        option.push(elements.options);
    })
    const unique = new Set()
    for (let i =0; i<10;i++){
        let rand = Math.floor(Math.random()*20+0);
        let input = quest[rand]
        unique.add(input);
        // console.log(typeof quest[rand])
        console.log('Inside the loop, quest: ' + input)
        console.log('Inside the loop, unique: ' + unique)
    }
    resp.send(unique)
}catch(e) {
    console.log(e)
}
1
  • What values are you getting for rand? Could rand be out of the bounds of quest? I'm not seeing anything that limits the value of rand to be within the bounds of quest. Commented Dec 20, 2021 at 18:37

1 Answer 1

1

It's probably working, but a Set can get converted into an empty object when it's getting JSONed. That is, there's no "standard" representation for a Set in JSON, so you get a "{}". Convert it into an array first:

resp.send(Array.from(unique));
Sign up to request clarification or add additional context in comments.

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.