Using Node, Express and Multer to upload a file to MongoDB? What should I use, Multer or GRIDfs?

0

Hi, I'm running a final project in Heroku using MongoLab and Multer to upload photos, it seems to work fine, but after a few hours or less the photos disappear from the page, but leave the img placeholder, the symbol appears when the browser can not find the image. In fact I open the console and that message appears that the file can not find.

It's this Heroku or Mongolab question or it's just the free mongoLab account. It also does not matter if there is a photo loaded or a dozen still disappear after a while.

Is there a hack, a complicated line of code or something I did wrong with Multer or my database? I have done a deep research on the web and Stack-Overflow I have found something called GRIDfs but I do not know if that will work. Am I on the right path?

This is my code in Node:

var express        = require("express"),
    body_parser    = require("body-parser"),
    multer         = require("multer"),
    mongoose       = require("mongoose");

    var app = express();

    //connect my local database to MongoLab database   

    var url = process.env.DATABASE || "mongodb://localhost/black_hat";

    mongoose.connect(url);

This is Multer's configuration:

var storage = multer.diskStorage({
destination: function(req, file, callback){
    callback(null, "./public/uploads");
},
filename: function(req, file, callback){
    callback(null, Date.now() + file.originalname);
}
  });

var upload = multer({storage : storage}).single("image");

This is the route where I upload the image:

app.post("/fotos_videos", isLoggedIn, function(req, res) {
    upload(req, res, function(err) {
        if(err){
            return res.end("Error uploading file");
        }
        // get data from form
        var newName = req.body.name,
            newCover = "/uploads/" + req.file.filename;
        // Packing into an object
        var newCollage = {
            name : newName,
            cover: newCover
        };
        //create new album
        Collage.create(newCollage, function(err, collage) {
            if(err){
                console.log(err);
            } else {
                // console.log(collage);
                res.redirect("/fotos_videos");
            }
        });
    });
});

The way I sent the image:

<div style="width=30%; margin: 30px auto" >
        
        <form action="/fotos_videos" method="POST" enctype="multipart/form-data">
            <div class="form-group">
                <label>Album Name</label>
                <input type="text" name="name" placeholder="Album Name" class="form-control">
            </div>
            <div class="form-group">
                <label>Choose Album Cover</label>
                <input type="file" name="image" placeholder="image url" class="form-control" required>
            </div>
            
            <div class="form-group">
                <button class="btn btn-lg btn-primary btn-block">Upload</button>
            </div>
        </form>
        
        <a href="/fotos_videos">Back</a>
        
    </div>

My mongoose model:

var mongoose = require("mongoose");

// Schema 
var collageSchema = new mongoose.Schema({
    name : String,
    cover : String,
    photos: [{
        type: mongoose.Schema.Types.ObjectId,
        ref: "Photo"
    }] 
});

var Collage = mongoose.model("Collage", collageSchema);

module.exports = Collage;

So I repeat everything is fine to pricipio I deploy the form where the image I want to upload asks me then redirects me to the page where that image unfolds and it's fine ... for a few hours because afterwards the image disappears. Leei and I found that multer does not upload the images to the database that I must use GRIDfs for do not know if that information is completely correct?

    
asked by Carlos Moreno 20.01.2017 в 18:10
source

1 answer

0

It is likely that you already know the reason, but for those who arrive looking for the answer:

heroku does not allow you to store files other than the app that you are deploying to your service. after a while they will delete it automatically, an alternative is to use cloudinars that have an api, upload the file with your node app, and send it

    
answered by 07.08.2017 в 20:25