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

标签 node.js couchdb

我正在编写一个应用程序,该应用程序从 CouchDB 文档中提取附件,将其写入磁盘,对其进行操作,然后将生成的文档存储回 CouchDB。第一部分表现出色。获取附件、将其写入磁盘并进行操作都没有问题。

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

  • 一台 CouchDB 服务器在某处运行或可以访问该服务器。
  • 名为演示的数据库
  • 所述数据库中的文档。
  • 以演示文稿中文档的 _id 命名的目录
  • 该目录中的 PNG。
  • Node 模块
    • fs
    • 路径
    • mime
    • 全局
    • txn

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

将程序另存为uploadPNG.js。使用此命令运行

./uploadPNG.js --id 目录名称

它应该遍历所有文件并将其添加到与目录具有相同 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
});

最佳答案

我认为您需要对附件正文进行 Base64 编码。另外,我认为附件内容存储在 .data 下,而不是 .body 下。

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

关于node.js - 在 Node.js 中使用 TXN 将附件上传到 CouchDB 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18945757/

相关文章:

node.js - Laravel Elixir 命令的异步执行

windows-10 - 父子连接时系统卡住

node.js - Couchdb 套接字在 View 中挂起

macos - Couch DB安装在Mac OSx Lion上不起作用

node.js - 您使用 node.js 见过或制作过哪些类型的应用程序?

javascript - 如何检查javascript中来自服务器的数据中是否存在 key ?

javascript - 检查流是否包含数据

javascript - 计算 JavaScript/NodeJS/Underscore 中的哈希值

CouchDB:是否可以访问过滤器功能内的链接文档?

mysql - 使用 PouchDB-CouchDB 和 MySQL 的移动应用程序