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)
}
CodePudding user response:
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));