在Node.js问题中使用TXN上传附件到CouchDB

我正在编写一个应用程序,它从CouchDB文档中提取附件,将其写入磁盘,对其进行处理,然后将生成的文档存储回CouchDB。 第一部分工作出色。 没有问题得到附件,写入磁盘并操纵它。

要复制下面的错误,您需要以下内容:

  • 一个CouchDB服务器运行某个地方或访问一个。
  • 一个名为演示的数据库
  • 所述数据库中的文档。
  • 在演示文稿中为文档的_id命名的目录
  • 在所述目录中的PNG。
  • 节点模块
    • FS
    • path
    • 哑剧
    • glob的
    • TXN

更改_workdir的位置以匹配您创build目录的位置。

将程序另存为uploadPNG.js 。 用这个命令运行

./uploadPNG.js --id directoryName

它应该通过并将所有文件添加到与目录具有相同ID的CouchDB文档。 下面的代码实际上是拉文件。 它只是不添加附件,我不知道为什么。 这是由运行程序导致的错误。

任何人都可以帮助我得到这个工作,我认为它应该?


错误

 { [Error: CouchDB error: {"error":"unknown_error","reason":"function_clause"}] statusCode: 500, error: 'unknown_error', reason: 'function_clause' } Error: CouchDB error: {"error":"unknown_error","reason":"function_clause"} at /Users/digilord/projects/superslick/couch_worker/node_modules/txn/lib/lib.js:59:18 at Request._callback (/Users/digilord/projects/superslick/couch_worker/node_modules/txn/lib/lib.js:125:12) at Request.self.callback (/Users/digilord/projects/superslick/couch_worker/node_modules/txn/node_modules/request/index.js:142:22) at Request.EventEmitter.emit (events.js:98:17) at Request.<anonymous> (/Users/digilord/projects/superslick/couch_worker/node_modules/txn/node_modules/request/index.js:856:14) at Request.EventEmitter.emit (events.js:117:20) at IncomingMessage.<anonymous> (/Users/digilord/projects/superslick/couch_worker/node_modules/txn/node_modules/request/index.js:808:12) at IncomingMessage.EventEmitter.emit (events.js:117:20) at _stream_readable.js:895:16 at process._tickCallback (node.js:415:13) 

uploadPNG.js

 #!/usr/bin/env node var fs = require('fs'), path = require('path'), mime = require('mime'), glob = require("glob"); var txn = require('txn').defaults({"timestamps":true, "couch":"http://localhost:5984"}); var presentations_txn = txn.defaults({"db":"presentations", "delay":1000, "timeout":30000}); var argv = require('optimist') .usage('Usage: $0 --id [Document ID]') .demand(['id']) .argv; var _doc_id = argv.id; var _workdir = path.join(path.resolve('/Volumes/ramdisk'), 'work'); var _docdir = path.join(_workdir, _doc_id); var _doc = null var addAttachments = function(doc, to_txn){ console.log(doc); console.log('Initial document fetched for: ', doc._id); console.log('Ready to push files to CouchDB for: ', _doc_id); var _globPattern = _docdir + '/*.png'; var _pngs = glob.sync(_globPattern); // console.log('PNGs: ', _pngs); _pngs.forEach(function(_file){ var _filecontents = fs.readFileSync(_file); var _mime_type = mime.lookup(_file); var _filename = _file.split('/').pop(); console.log("Processing Attachment: ", _filename) if(typeof doc._attachments[_filename] == 'object'){ doc._attachments[_filename].content_type = _mime_type; doc._attachments[_filename].body = _filecontents; } else { doc._attachments[_filename] = {}; doc._attachments[_filename].content_type = _mime_type; doc._attachments[_filename].body = _filecontents; } // uploadAttachment(_file, _docdir, _doc_id) }); doc.processed = true; to_txn() } presentations_txn({"id": _doc_id}, addAttachments, function(error, newData) { if(!error) return console.log("Processed " + _doc_id + " to state: " + newData.processed); // These errors can be sent by Txn. if(error.timeout) return console.log("Gave up after MANY conflicts"); if(error.conflict) return console.log("addAttachments never completed. Troubleshoot and try again"); console.log(error); throw error; // Unknown error }); 

我认为你需要base64编码你的附件身体。 另外我认为附件内容存储在.data下,而不是.body

 doc._attachments[_filename].content_type = _mime_type; doc._attachments[_filename].data = _filecontents.toString('base64');