DEV Community

Discussion on: Build a Quiz REST API with NodeJS

Collapse
 
raynerray profile image
RaynerRay

hi Carlos , how can one include images to the questions , like a question that has both text and image , thank you

Collapse
 
raymag profile image
Carlos Magno

Hello RaynerRay!
That's a interesting question. I'd suggest you to add a new string field in the question model (7th step of the tutorial). That way you can save the path or the link of the image in the database. It would be something like this (Note the "image" field):

const QuestionSchema = new mongoose.Schema({
    description: String,
    image: {
        type: String,
        required: false
    },
    alternatives: [
        {
            text: {
                type: String,
                required: true
            },
            isCorrect: {
                type: Boolean,
                required: true,
                default: false
            }
        }
    ]
})
Collapse
 
raynerray profile image
RaynerRay

Thanks mate, if it works, i'll post here

Collapse
 
raynerray profile image
RaynerRay

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 👏

Thread Thread
 
raymag profile image
Carlos Magno

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:

const QuestionSchema = new mongoose.Schema({
    description: String,
    image: {
        type: String,
        required: false
    },
    alternatives: [
        {
            text: {
                type: String,
                required: true
            },
            isCorrect: {
                type: Boolean,
                required: true,
                default: false
            },
            image: {
                type: String,
                required: false
            },
        }
    ]
})

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 question
router.post('/questions', async (req, res) => {
    try {
        const { description } = req.body
        const { image } = req.body
        const { alternatives } = req.body

        const question = await Question.create({
            description,
            alternatives,
            image
        })

        return res.status(201).json(question)
    } catch (error) {
        return res.status(500).json({"error":error})
    }
})

// update one quiz question
router.put('/questions/:id', async (req, res) => {
    try {
        const _id = req.params.id 
        const { description, image, alternatives } = req.body

        let question = await Question.findOne({_id})

        if(!question){
            question = await Question.create({
                description,
                alternatives
            })    
            return res.status(201).json(question)
        }else{
            question.description = description
            question.alternatives = alternatives
            question.image = image
            await question.save()
            return res.status(200).json(question)
        }
    } catch (error) {
        return res.status(500).json({"error":error})
    }
})
Thread Thread
 
raymag profile image
Carlos Magno

That way, when you are creating a new question, you can pass data like this:
creating a question

If you have more questions about this, feel free to send me a DM as well. Have a good day!

Thread Thread
 
raynerray profile image
RaynerRay

Many thanks, my mistake i was trying to upload images to the database, THANK YOU 🙏