我正在使用nodejs mongodb mongoose和gridfs.当我试图通过它获取一个文件的filname everthing工作得很好,如果我想通过id得到它我得到错误:你想要阅读的文件不存在.我在下面的代码console.log("res.pic_id:"+ res.pic_id)我得到了正确的ObjectId.这是代码:
var GridFS = require('GridFS').GridFS;
var myFS = new GridFS('db');
var fs = require('fs')
var Profile = db.model('Profile');
Profile.findOne({'_id' : clientID},['_id', 'username','pic_id','pic_filename'],function(err, res){
if (err) {
console.log("ERROR serching user info: " + err);
callback(JSON.stringify(JSONRes(false, err)));
}
else {
if (res) {
console.log("res.pic_id : " + res.pic_id);
myFS.get(res.pic_id,function(err,data){
if (err)
console.log("ERROR "+err)
else {
callback(data);
}})
};
}
else {
callback(JSON.stringify(JSONRes(false, err)));
}
}
})
Run Code Online (Sandbox Code Playgroud)
谢谢!