Attach metadata infos to a Gridfs document file via req.query in API - Node.js Expresss.js MongoDB Multer Gridfs stream

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Attach metadata infos to a Gridfs document file via req.query in API - Node.js Expresss.js MongoDB Multer Gridfs stream



I'm struggling to attach metadata infos to a dfile before actually store it in mongoDB. I'm using:


const express = require("express");
const router = express.Router();
//File managing with gridfs
const mongoose = require('mongoose');
const path = require('path');
const crypto = require('crypto');
const multer = require('multer');
const GridFsStorage = require('multer-gridfs-storage');
const Grid = require('gridfs-stream');
const fs = require('fs');



As libraries, and then for the store engine :


const conn = mongoose.createConnection(mongoURI);
//TODO : test the api for the files
let gfs;

conn.once('open', () =>
// Init stream
gfs = Grid(conn.db, mongoose.mongo);
gfs.collection('uploads');
);

// Create storage engine
const storage = new GridFsStorage(
url: mongoURI,
gfs:gfs,
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);
);
);
,
metadata: (req, file, cb) =>
const metadata =
originalname: file.originalname,
// get this information somehow
restaurantID :req.query.restaurantID,
category : req.query.category
;
cb(null, metadata);

);
const upload = multer( storage: storage );



Then for the post API i dont have that much yet just a code to update another mongoDB document with a reference to the file that works


router.post('/upload', upload.single("file"), (req, res) =>

//All the code to update



What I need is a way to store infos passed in req.query and put it into the metadata of the gridFS file collection in order to make some query to find the right files in the future and then save it obviusoly. This for every file that I upload with this API. Thank you guys.









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard