This has been getting me all day. I can find the actual file with it's _id using gfs.exist(), but then when i run to the next line of code i get an error everytime and the mongoose connection crashes. This seems so simple yet nothing has worked so far.
my code:
/**
* Created by foolishklown on 10/2/2016.
*/
var Grid = require('gridfs-stream'),
User = require('../../models/user'),
mongoose = require('mongoose');
module.exports = function(id, ref, type, res) {
console.log(ref.green);
Grid.mongo = mongoose.mongo;
var conn = mongoose.createConnection('mongodb://localhost/media');
conn.once('open', function () {
var gfs = Grid(conn.db);
gfs.exist({_id: ref}, function(err, found) {
if(err) {
console.error('error finding file'.red);
} else {
console.info('found file', found);
gfs.files.remove({_id: ref }, function(err) {
if(err) {
console.error('error removing that file');
process.exit(1);
} else {
console.info('removed file: ', found.green);
deleteFromUserDb(id, type, ref);
res.status(200).send({id: id, type: type, ref: ref});
}
});
}
});
});
conn.close();
function deleteFromUserDb(userId, fileType, refId) {
var userConn = mongoose.createConnection('mongodb://localhost/mean-auth', (error) => {
if(error) {
console.error('Error connecting to the mean-auth instance'.red);
process.exit(1);
} else {
User.findById(userId, (err, doc) => {
if(err) {
console.error('Error finding user with id: ', uid);
process.exit(1);
} else {
console.log('original doc: ', doc);
doc.removeMedia(fileType, refId);
doc.save();
console.log('new doc: ', doc);
}
})
}
});
}
};
i have tried using gfs.files.remove({_id: ref}, function(.....) to no avail
i have also tried using gfs.files.remove({_id: ref}, {_id: 1}, function(....)
i have also tried both of the above using gfs.remove() without gfs.files.remove.....
it must be something simple but its been beating me up all day... thanks
new edit 10/15.......... i am now trying to use just the native mongodb driver. I can find the file both with the string, and casting it to an objectId. It seems like the operation completes no problem, but when i use the shell to see if the file has been deleted it still exists in both fs.files and fs.chunks. This one is killing me!
deleteFile: function(userId, fileType, objId, res) {
var ObjectId = require('mongodb');
var client = mongodb.MongoClient;
var _id = new ObjectId(objId);
client.connect(mediaUri, (err, db) => {
assert.equal(null, err);
db.collection('fs.files').find({_id: _id}, (err, doc) => {
if(err) {
console.error('error finding that document in files collection'.red);
} else {
console.info('found the document: ', doc);
console.info('the document type is: ', typeof(doc));
}
});
db.collection('fs.chunks').find({_id: _id }, (err, doc) => {
if(err) {
console.error('error finding that document in the chunks collection'.red);
} else {
console.info('found the document(s): ', doc);
console.info('the document type is: ', typeof(doc));
}
});
db.collection('fs.files').deleteOne({_id: _id}, (err, doc) => {
console.log('document returned for deletion is: ', doc);
});
db.collection('fs.chunks').deleteOne({_id: _id}, (err, doc) => {
console.log('documents deleted: ', doc);
res.status(200).send({id: userId, type: fileType, ref: objId});
});
db.close();
})
}