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?