我正在编写一个应用程序,它从 CouchDB 文档中提取附件,将其写入磁盘,对其进行操作,然后将生成的文档存储回 CouchDB。第一部分工作出色。获取附件、将其写入磁盘和操作它没有问题。
要复制以下错误,您需要以下内容:
- 在某处运行或访问其中的 CouchDB 服务器。
- 一个名为presentation的数据库
- 所述数据库中的文档。
- 以演示文稿中文档的 _id 命名的目录
- 所述目录中的PNG。
- 节点模块
- fs
- 小路
- 哑剧
- 球体
- txn
更改 _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)
上传PNG.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
});