这一整天都让我感到高兴。我可以使用gfs.exist()找到它的实际文件,但是当我运行到下一行代码时,我每次都会收到错误并且mongoose连接崩溃。这似乎很简单,但到目前为止还没有任何工作。
我的代码:
/**
* 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);
}
})
}
});
}
};
我尝试过使用gfs.files.remove({_ id:ref},函数(.....)无效
我也尝试过使用gfs.files.remove({_ id:ref},{_ id:1},function(....)
我也尝试过使用gfs.remove()而不使用gfs.files.remove .....以上
它必须是简单的东西,但它一直在殴打我... 感谢新编辑10/15 .......... 我现在正试图使用本机mongodb驱动程序。我可以使用字符串找到该文件,并将其转换为objectId。看起来操作完成没有问题,但是当我使用shell查看文件是否已被删除时,它仍然存在于fs.files和fs.chunks中。这个杀了我!
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();
})
}
答案 0 :(得分:0)
我做错了,并且使用了错误的方法。 在为mongodb.ObjectID()而不是mongodb.ObjectiD()使用新的构造函数之后,我取得了一些成功,而且我正在调用一些错误的方法。我查阅了文档并理顺了事情。
我的CRUD操作的最终代码是:
{{1}}
答案 1 :(得分:0)
如果您按以下方式导入ObjectId
对象:
const ObjectId = mongoose.Types.ObjectId;
与常规导入一样,在文件顶部,您可以按以下方式编写删除操作:
const mongoose = require('mongoose');
const Grid = require('gridfs-stream');
const ObjectId = mongoose.Types.ObjectId
Grid.mongo = mongoose.mongo // assign the mongo driver to the Grid
const mongooseURI = 'mongodb://localhost/media';
const conn = mongoose.createConnection(mongooseURI, {useNewUrlParser:true});
conn.once('open', () => {
const gfs = Grid(conn.db);
gfs.exist({_id: ref}, (err, found) => {
if(err) {
console.error('error finding file'.red);
} else {
console.info('found file', found);
gfs.files.deleteOne({_id:new ObjectId(ref)}, (err, result) => {
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});
}
});
}
});
希望这会有所帮助!