我正在编写一个应用程序,它从CouchDB文档中提取附件,将其写入磁盘,对其进行操作,然后将生成的文档存储回CouchDB。第一部分运作出色。获取附件没有问题,将其写入磁盘并进行操作。
要复制下面的错误,您需要以下内容:
更改_workdir的位置以匹配您创建目录的位置。
将程序保存为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)
#!/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
});
答案 0 :(得分:1)
我认为您需要对附件体进行base64编码。另外,我认为附件内容存储在.data
下,而不是.body
。
doc._attachments[_filename].content_type = _mime_type;
doc._attachments[_filename].data = _filecontents.toString('base64');