bradtraversy/mongo_file_uploads

(node:19300) UnhandledPromiseRejectionWarning: Error: Username containing an unescaped at-sign

vijoytechy opened this issue · 1 comments

Unable to Run this Code ,getting following error

> (node:19300) UnhandledPromiseRejectionWarning: Error: Username containing an unescaped at-sign
    at parseConnectionString (/home/vijo/Music/MEAN/mongo_file_uploads-master/node_modules/mongodb/lib/url_parser.js:176:13)
    at parseHandler (/home/vijo/Music/MEAN/mongo_file_uploads-master/node_modules/mongodb/lib/url_parser.js:119:14)
    at module.exports (/home/vijo/Music/MEAN/mongo_file_uploads-master/node_modules/mongodb/lib/url_parser.js:25:12)
    at Promise (/home/vijo/Music/MEAN/mongo_file_uploads-master/node_modules/mongoose/lib/connection.js:338:5)
    at new Promise (<anonymous>)
    at NativeConnection.Connection.openUri (/home/vijo/Music/MEAN/mongo_file_uploads-master/node_modules/mongoose/lib/connection.js:337:19)
    at Mongoose.createConnection (/home/vijo/Music/MEAN/mongo_file_uploads-master/node_modules/mongoose/lib/index.js:167:17)
    at Object.<anonymous> (/home/vijo/Music/MEAN/mongo_file_uploads-master/app.js:22:23)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3
(node:19300) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:19300) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

`const express = require('express');
const bodyParser = require('body-parser');
const path = require('path');
const crypto = require('crypto');
const mongoose = require('mongoose');
const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const methodOverride = require('method-override');

const app = express();

app.use(bodyParser.json());
app.use(methodOverride('_method'));
app.set('view engine', 'ejs');

const mongoURI = 'mongodb://v:v@123@ds111568.mlab.com:11568/mongouploads';


const conn = mongoose.createConnection(mongoURI);

let gfs;

conn.once('open', () => {

gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('uploads');
});

const storage = new GridFsStorage({
url: mongoURI,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err);
}
const filename = buf.toString('hex') + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'uploads'
};
resolve(fileInfo);
});
});
}
});
const upload = multer({ storage });

app.get('/', (req, res) => {
gfs.files.find().toArray((err, files) => {

if (!files || files.length === 0) {
  res.render('index', { files: false });
} else {
  files.map(file => {
    if (
      file.contentType === 'image/jpeg' ||
      file.contentType === 'image/png'
    ) {
      file.isImage = true;
    } else {
      file.isImage = false;
    }
  });
  res.render('index', { files: files });
}

});
});

app.post('/upload', upload.single('file'), (req, res) => {

res.redirect('/');
});

app.get('/files', (req, res) => {
gfs.files.find().toArray((err, files) => {

if (!files || files.length === 0) {
  return res.status(404).json({
    err: 'No files exist'
  });
}


return res.json(files);

});
});

app.get('/files/:filename', (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {

if (!file || file.length === 0) {
  return res.status(404).json({
    err: 'No file exists'
  });
}

return res.json(file);

});
});

app.get('/image/:filename', (req, res) => {
gfs.files.findOne({ filename: req.params.filename }, (err, file) => {

if (!file || file.length === 0) {
  return res.status(404).json({
    err: 'No file exists'
  });
}


if (file.contentType === 'image/jpeg' || file.contentType === 'image/png') {

  const readstream = gfs.createReadStream(file.filename);
  readstream.pipe(res);
} else {
  res.status(404).json({
    err: 'Not an image'
  });
}

});
});

app.delete('/files/:id', (req, res) => {
gfs.remove({ _id: req.params.id, root: 'uploads' }, (err, gridStore) => {
if (err) {
return res.status(404).json({ err: err });
}

res.redirect('/');

});
});

const port = 5000;

app.listen(port, () => console.log(Server started on port ${port}));`

Replace the @ with %40 in your password and it will work :)