vendredi 3 avril 2015

Save file in an array using gridfs-stream

My web app allows the user to create a word document, then this document is saved in a collection called 'Files' using grid-stream. 'Files' is structured the following way:


Files:



var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var db = mongoose.connection;

var FileSchema = new Schema({
userID:{type: String},
//names:{type: String},
file:[{type: Schema.Types.Mixed}]
},{collection: 'Files'});
module.exports = mongoose.model('Files', FileSchema);


Everytime the user saves the word document, I want to save it in the array, this is because the user can have more the one word document.


Here is my save method:



var Files = require('C:/Users/mississauga/OneDrive/workspace/Editor/Models/files.js');
var mongoose = require('mongoose');
var User = require('C:/Users/mississauga/OneDrive/workspace/Editor/Models/users.js');
var db = mongoose.connection;
db.safe = {w: 1};
//var GridStore = mongoose.mongo.GridStore;
var Grid = require ('gridfs-stream');
var mongo = mongoose.mongo;
var gfs = new Grid(db, mongo);
//var fs = require ('fs');

var SaveFile = function(req, names, contents, userid){
console.log("inside SaveFile");
console.log("this is the name: " + names);
console.log("this is the content: " + contents);
//console.log(" the user: " + req.session.user);
//var gs = new GridStore (db, names, "w",gsOptions);
console.log("The username : " + userid);

Files.findOne({'userID' : userid },
function(err, user) {
// In case of any error, return using the done
//method
if (err){
console.log('Error in Finding user: '+err);
return done(err);
}
// already exists
if (user) {
console.log("inside user");
Files.findOne({'file': {'name':names}},
function(err, filename){
if(filename){
console.log("file already Exists");
}
else{
var writeStream = req.pipe(gfs.createWriteStream({
filename: names,

}));
console.log("Passed writestrea");

writeStream.on('close', function(){
return res.status(200).send({
message:'Success'
});
});
writeStream.write(contents);
writeStream.end();
user.save(function(err) {
console.log("found user");
if (err){
console.log('Error in Saving file:'+err);
throw err;
}
console.log('File Saved');
return filename;
});
}
}


);
}
else {
console.log("inside else");

var writeStream = gfs.createWriteStream({
filename: names,

});

console.log("Passed writestrea");

writeStream.on('close', function(){
return res.status(200).send({
message:'Success'
});
});
req.pipe(writeStream);
writeStream.write(contents);
writeStream.end();

var newFile = new Files();
newFile.userID= userid;
writeStream.on('close', function(file){
newFile.file = file;
});


newFile.save(function(err) {
console.log("found user");
if (err){
console.log('Error in Saving file:'+err);
throw err;
}
console.log('File Saved');
return filename;
});

}
});


console.log("passed Grid");
}
module.exports = SaveFile;


When I run the code the file is not saved, this is what I see in the database:


As you can see there is one document in the collection Files


Inside file collection. How ever it is just the userid that is saved, the file array is empty


How do I save the file to the array?


Aucun commentaire:

Enregistrer un commentaire