0

So basically i have JSON object from mongoDB as listed below:

{
  "_id": {
    "$oid": "634e110e2f20d317888ef895"
  },
  "userId": {
    "$oid": "63490f1a8de6afa4c46437ec"
  },
  "formId": {
    "$oid": "634d2161810c1cf723d7b40a"
  },
  "answer": {
    "634d2161810c1cf723d7b40c": "12:10",
    "Makan_Makan": "12:10",
    "634d23494970913dbce45132": "john@nabatisnack.com",
    "asdasdsa": "john@nabatisnack.com"
  },
  "createdAt": 1666060558,
  "updatedAt": 1666060558,
  "__v": 0
}

i try to iterate it with javascript code to consume and send it to frontend (Vuejs) with code as below,

const form = await Form.findOne({
        _id: req.params.formId,
        userId: req.JWT.id,
      }).populate("answers");
      if (!form) {
        throw { code: 404, message: "FORM_NOT_FOUND" };
      }
      console.log("form untuk response", form.answers);
      const summaries = form.questions.map((question) => {
        let summary = {
          type: question.type,
          questionId: question.id,
          question: question.question,
          answers: form.answers.map((answerss) =>
            // answer[question.id]
            console.log(answerss)
          ),
        };

    return summary;
  });

i have try to iterate it with map function from javscript but it turns out that the answers is still undefined while actually it has value from the mongo with the JSON, here is the request result from BE that will be transferred to FE

Summary untuk response [
  {
    type: 'Email',
    questionId: new ObjectId("634d2161810c1cf723d7b40c"),
    question: 'Makan Makan',
    answers: [
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined
    ]
  },
  {
    type: 'Email',
    questionId: new ObjectId("634d23494970913dbce45132"),
    question: 'asdasdsa',
    answers: [
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined, undefined,
      undefined
    ]
  }
]

any help on this?

JS24
  • 1
  • 2
  • 14

0 Answers0