2012-04-10 82 views
0

让node.js和gridfs好玩并不容易。在我尝试的所有事情中,我尽可能接近我所能得到的知识,并了解当前支持的功能将允许的范围。没有错误 - 仍然没有写

(以下咖啡,使用http://js2coffee.org/去的JS,反之亦然),

util = require("util") 
mongodb = require("mongodb") 
GridStore = mongodb.GridStore 
parse = (options) -> 
    opts = {} 
    opts = options[0] if options.length > 0 
    opts.metadata = {} unless opts.metadata 
    opts 

db = new Db("local", new Server("127.0.0.1", 27017, 
    auto_reconnect: false 
    poolSize: 1 
), 
    native_parser: false 
) 
db.open() 
putFile = (path, name, options, fn) -> 
    options = parse(options) 
    options.metadata.filename = name 
    new GridStore(db, name, "w", options).open (err, file) -> 
    return fn(err) if err 
    file.writeFile path, (err, fn) -> 
     file.close() 

opts = content_type: "plain/text" 
myfileupload = putFile("myfile.txt", "known_hosts", opts) 
db.close() 

然而奇怪的使用apt-get安装的mongodb-10gen公司在Ubuntu 11.10,我的文件没有保存。并没有错误信息来帮助我理解原因。

我接近相信,我所读到的关于gridfs和nodejs的一切都只是一个残酷的笑话,我永远不会看到这项工作。请帮忙。

+0

如果你有安装MongoDB的问题,我有同样的问题,看到我问这个问题:http://stackoverflow.com/questions/10046101/installing-and-starting-mongodb-for-php-on- ubuntu-12-04 – 2012-04-10 18:20:15

+0

哦,安装没有问题...我已经完成了一千次以上。对于Ubuntu的打包版本最有趣,但我发现了太多矛盾的元素出现,我不知道为什么他们甚至打扰它包装。我看了一下你的帖子 - 也评论过了;-) – 2012-04-10 18:43:56

回答

2

我怀疑是异步问题。您在致电putFile后立即致电db.close(),因此db.close()正在运行之前GridStoreopen回调引发火灾,对不对?似乎它可能是一个问题。尝试将db.close()转换为与file.close()相同的回调。

+0

谢谢你指出了。将db.close()移入与file.close()相同的回调是有意义的。不过,我想我还有另一个问题,因为我将db.close()放在同一回调中的file.close()之后,但仍然看到它运行时没有错误,也没有保存。 (使用C#从远程Windows客户端工作,所以它不是我的MongoDB,这是问题)。不管怎么说,还是要谢谢你。 – 2012-04-10 20:10:15

+0

我能够通过使用“return file.writeFile(path,fn);”根据我在这里找到的示例:github.com/jamescarr/nodejs-mongodb-streaming – 2012-04-19 08:07:03

+0

@OnesandZeroes您是如何真正解决这个问题的?我一直在为此奋斗了近一天。奇怪的是,在我的猫鼬日志中,我看到猫鼬正在查找 猫鼬:fs.files.find({filename:'81156f486d17269f524b4b313a808df7.jpg'}){readPreference:'primary',w:'majority'} Mongoose: fs.chunks.find({n:0,files_id:ObjectId(“54bb815b1b1bdbd2686f20b0”)}){readPreference:'primary',w:'majority'} 而不是像我期待的那样插入到DB。你觉得我错过了什么? – shanks 2015-01-18 09:58:39