Lia*_*atz 4 mongodb node.js gridfs
我正在使用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)
谢谢!
我遇到了类似的问题.问题结果是我使用的是ObjectID的字符串表示而不是真正的ObjectID.而不是这个:
var gridStore = new GridStore(db, '51299e0881b8e10011000001', 'r');
Run Code Online (Sandbox Code Playgroud)
我需要这样做:
var gridStore = new GridStore(db, new ObjectID('51299e0881b8e10011000001'), 'r');
Run Code Online (Sandbox Code Playgroud)
小智 4
您必须将其存储为文件名或 object.id 作为主键。最好的方法是使用 ObjectID 作为标识符来存储它,然后将文件名添加到元数据并使用它进行查询。
查看文档中的第三个示例(这是在 mongoose 下的本机驱动程序的情况下)
http://mongodb.github.com/node-mongodb-native/api- generated/gridstore.html#open
| 归档时间: |
|
| 查看次数: |
4396 次 |
| 最近记录: |