0

Am developing an API for taking input information like title, description, and image or document. I have tested the API is working when I don't add the image/document part. How can I add this field to insert an image/document to the mongo DB? I can actually add info without the image/docs upload, How do I go about it?

Upload info controller

exports.upload = (req, res) =>{

const { title, category, duration, durationSys, description } = req.body
Project.findOne({ title }).exec((err, project) => {
    if (project) {
        return res.status(400).json({
            error: 'Choose a unique title for your Project'
        });
    }
});

let newProject = new Project({ title, category, duration, durationSys, description});

newProject.save((err, success) => {
    if (err) {
        console.log('ADDING PROJECT ERROR', err);
        return res.status(400).json({
            error: err
        });
    }
    res.json({
        message: 'Added successfully, check your projects'
    });
});

};

Tridib Dawn
  • 168
  • 1
  • 11
ephantus okumu
  • 178
  • 2
  • 11

1 Answers1

0

If you want to store the image in the MongoDB you can refer to this thread. Store images in a MongoDB database

Personally, I would recommend uploading the image/document to a server (preferably S3 Bucket) and just storing the link to the image/document in the Database.

Read more here regarding why it's better to not store images/document on Database rather just storing a link to the image/document.

Roshan Raj
  • 173
  • 2
  • 10
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](https://stackoverflow.com/review/late-answers/28309730) – Adam Marshall Feb 12 '21 at 18:12
  • 1
    Thank you for pointing it out. Will keep that in mind. – Roshan Raj Feb 13 '21 at 14:15