0

I am getting this error on Postman when I send request for uploading files on Feathersjs:

  {
        "name": "GeneralError",
        "message": "ENOENT: no such file or directory, open 'public/uploads/pic'",
        "code": 500,
        "className": "general-error",
        "data": {},
        "errors": {}
    }

My uploads.service.js:

const {Uploads} = require('./uploads.class');
const createModel = require('../../models/uploads.model');
const hooks = require('./uploads.hooks');
const multer = require('multer');

const storage = multer.diskStorage({
  destination: (_req, _file, cb) => cb(null, 'public/uploads'), // where the files are being stored
  filename: (_req, file, cb) => {
    console.log(_req.body);
    //cb(null, ${_req.body.name});
    cb(null, `${_req.body.name}`); //
  }, // getting the file name
});
const uploads = multer({
  storage,
  limits: {
    fieldSize: 1e8,
    fileSize: 1e7,
  },
});

module.exports = function(app) {
  const options = {
    Model: createModel(app),
    paginate: app.get('paginate'),
    multi: true,
  };

  // Initialize our service with any options it requires
  app.use(
    '/uploads',
    uploads.array('files'),
    (req, _res, next) => {
      const {method} = req;
      if (method === 'POST' || method === 'PATCH') {
        console.log(req.files);
        console.log(req.body);
        req.feathers.files = req.body.files;
        const body = [];
        for (const file of req.files)
          body.push({
            name: req.body.name,
            newNameWithPath: file.path,
          });
        req.body = method === 'POST' ? body : body[0];
      }
      next();
    },

    new Uploads(options, app),
  );

  // Get our initialized service so that we can register hooks
  const service = app.service('uploads');

  service.hooks(hooks);
};

This is my uploads.model.js:

module.exports = function(app) {
  const modelName = 'uploads';
  const mongooseClient = app.get('mongooseClient');
  const {Schema} = mongooseClient;
  const schema = new Schema(
    {
      name: {type: String, required: true},
    },
    {
      timestamps: true,
    },
  );

  // This is necessary to avoid model compilation errors in watch mode
  // see https://mongoosejs.com/docs/api/connection.html#connection_Connection-deleteModel
  if (mongooseClient.modelNames().includes(modelName)) {
    mongooseClient.deleteModel(modelName);
  }
  return mongooseClient.model(modelName, schema);
};

I really cannot figure out where exactly the problem is. According to me it is supposed to make the folder automatically when I upload the file.

I would really appreciate some help. Thank you in advance.

1 Answer 1

1

It was my own mistake. I made the uploads folder inside public folder myself and now it's working.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.