我使用 busboy 模块通过下面的 coffeeScript 代码解析多部分请求。有时,问题是针对包含一个文件的请求多次调用“数据”处理程序。这意味着我需要对每个尺寸求和才能计算出整个尺寸。除了 on 'file' 处理程序中的文件对象似乎不包括大小信息。
如何在不计算每个部分的情况下获得整个尺寸?
提前致谢-
busboy.on 'file', (fieldname, file, filename, encoding, mimetype) ->
filename = "#{Meteor.uuid()}.jpg"
dir = "#{HomeDir()}/data/profile"
saveTo = path.join dir, filename
file.pipe fs.createWriteStream saveTo
files.push
filename: filename
path: saveTo
fileSize: data.length
file.on 'data', (data) ->
# this data handler called several times
files.push
filename: filename
path: saveTo
fileSize: data.length
file.on 'end', ->
console.log 'file finished'
Run Code Online (Sandbox Code Playgroud) MongoInternals.RemoteCollectionDriver("mongodb://#{server.ip}:#{server.port}/#{server.dbName}")
Run Code Online (Sandbox Code Playgroud)
如果我调用多个远程 MongoDB 方法并且如果有同名的集合,Meteor 会抛出类似这样的错误,“collectionName/insert is already existing...”
我认为 Meteor 在内部创建每个集合的方法,以便控制每个集合,但是由于某些原因我需要一次控制多个 MongoDB。
我怎样才能避免这种情况?
此外,我意识到我可以像这样直接使用 Npm Mongo 驱动程序,而无需涉及任何 NPM 包。
var MongoClient = MongoInternals.NpmModules.mongodb.module.MongoClient;
// Connection URL
var url = 'mongodb://localhost:27017/myproject';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
console.log("Connected correctly to server");
try {
var collection = db.collection('documents');
collection.find({}).toArray(function(err, docs){
console.log(err);
console.log(docs);
});
}
catch(err) {
console.log(err);
}
db.close();
});
Run Code Online (Sandbox Code Playgroud)
但这仍然迫使我使用 Node.js 回调样式来控制每个数据库。有什么想法可以避免这种情况吗?