无法从node.js读取gridstore中的数据

时间:2015-05-13 07:03:14

标签: node.js mongodb gridfs

我正在GridStore中编写一个文件并关闭它。 fs.files正在保存这些数据。在尝试读取这些数据后,mongodb发出以下错误

{ name: 'MongoError',  message: 'file with id 5520db57a1233e72e786fc6 not opened for writing' }

这是我的代码

var gs = new GridStore(global.gridStoreDB , new ObjectID() , "w",{
          "metadata":{
            "author": appId
           }
        });

用于将文件写入db

gs.open(function(err, gs){
            if(err){
                console.log("error upload file");
                deferred.reject(err);
            }else {
                console.log("this file was uploaded at "+gs.contentType+"  "+gs.length+"  "+gs.metadata);
                gs.writeFile( filePath, function(err, gs){ 
                    if(err){
                        console.log("error writing file");
                        deferred.reject(err);
                    }else{
                        gs.close(function(err, gs){
                            console.log("this file was written at "+gs.contentType+"  "+gs.length+"  "+gs.metadata);
                            gs['_url'] = global.keys.fileUrl + gs._id;
                            deferred.resolve(gs);
                        });
                    }
                });
            }

            fs.unlink(filePath, function(err){
                if (err) {
                    // Could not delete file;
                }else {
                    // file deleted from uploads
                }
            });
        });

这里我收到gs2.open()的错误,从DB读取文件,

var gs2 = new GridStore(global.gridStoreDB, fileId, "r");
        gs2.open(function(err, gs){
            if(err){
                console.log(err);
                deferred.reject(err);

            }else{
                gs2.read(global.gridStoreDb, ObjectID(fileId), function(err, fileData){
                    if(err){
                        console.log("Reading File " + err);
                        deferred.reject(err);
                    }else{
                        console.log("File Data : " + fileData);
                        gs2.close(function(err, result){
                            deferred.resolve(fileData);
                        });
                    }
                });
            }
        });

0 个答案:

没有答案