Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting storage error #2

Open
karthikvarmak1 opened this issue Apr 16, 2018 · 0 comments
Open

Getting storage error #2

karthikvarmak1 opened this issue Apr 16, 2018 · 0 comments

Comments

@karthikvarmak1
Copy link

I am getting the following response

{
    "error_code": 1,
    "err_desc": {
        "storageErrors": []
    }
}

http://localhost:3000/register/uploadprofile --> postman request

//GridFs --- multer
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var multer = require('multer');
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;
var Readable = require('stream').Readable;
var GridFsStorage = require('multer-gridfs-storage');

var RegisteredUserApi = require('../data/FoodCourtApi');
var express = require('express');
var router = express.Router();

var app = express();
app.use(bodyParser.json());

mongoose.Promise = global.Promise;
mongoose.connect('mongodb://localhost:27050/FoodCourtDatabase');

var conn = mongoose.connection;

conn.once('open', function () {
    console.log('Connected to DB.');
});
conn.on('error', function (err) {
    console.log('Connection error', err);
});

var gfs = Grid("FoodCourtDatabase");

var storage = GridFsStorage({
    gfs : gfs,
    filename: function (req, file, cb) {
        var datetimestamp = Date.now();
        cb(null, file.fieldname + '-' + datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length -1]);
    },
    /** With gridfs we can store aditional meta-data along with the file */
    metadata: function(req, file, cb) {
        cb(null, { originalname: file.originalname });
    },
    root: 'profiles' //root name for collection to store files into
});

// multer settings for single upload
var upload = multer({
    storage: storage
}).single('profilepict');

router.post('/uploadprofile', function (req, res) {
    upload(req,res,function(err){
        if(err){
             res.status(400).json({error_code:1,err_desc:err});
             return;
        }
         res.json({error_code:0,err_desc:null});
    });
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant