Hi Carlos , sorry man but have tried all i can it's not working, trying to store images in both questions and answers,for example so a question can have test question and image answers, please help 👏
Well, in order to add an image field for the question and for each alternative, you need to modify the model in Question.js. Here is my approach into this:
Like I said before, you don't upload the images to the database, you just upload the link to the image, so they need to be hosted in another place on internet before hand.
Also, after modifying the model, you also need to update the routes for creating and updating questions in routes.js. Something like this:
// create one quiz questionrouter.post('/questions',async(req,res)=>{try{const{description}=req.bodyconst{image}=req.bodyconst{alternatives}=req.bodyconstquestion=awaitQuestion.create({description,alternatives,image})returnres.status(201).json(question)}catch(error){returnres.status(500).json({"error":error})}})// update one quiz questionrouter.put('/questions/:id',async(req,res)=>{try{const_id=req.params.idconst{description,image,alternatives}=req.bodyletquestion=awaitQuestion.findOne({_id})if(!question){question=awaitQuestion.create({description,alternatives})returnres.status(201).json(question)}else{question.description=descriptionquestion.alternatives=alternativesquestion.image=imageawaitquestion.save()returnres.status(200).json(question)}}catch(error){returnres.status(500).json({"error":error})}})
Hi Carlos , sorry man but have tried all i can it's not working, trying to store images in both questions and answers,for example so a question can have test question and image answers, please help 👏
Well, in order to add an image field for the question and for each alternative, you need to modify the model in
Question.js
. Here is my approach into this:Like I said before, you don't upload the images to the database, you just upload the link to the image, so they need to be hosted in another place on internet before hand.
Also, after modifying the model, you also need to update the routes for creating and updating questions in
routes.js
. Something like this:That way, when you are creating a new question, you can pass data like this:
If you have more questions about this, feel free to send me a DM as well. Have a good day!
Many thanks, my mistake i was trying to upload images to the database, THANK YOU 🙏