1

CouchDB ドキュメントから添付ファイルを取得し、ディスクに書き込み、操作し、結果のドキュメントを CouchDB に保存するアプリケーションを作成しています。最初の部分は見事に動作します。添付ファイルの取得、ディスクへの書き込み、および操作に問題はありません。

以下のエラーを再現するには、次のものが必要です。

  • どこかで実行されている CouchDB サーバー、またはそのサーバーへのアクセス。
  • プレゼンテーションという名前のデータベース
  • 上記データベース内のドキュメント。
  • プレゼンテーション内のドキュメントの _id にちなんで名付けられたディレクトリ
  • 上記のディレクトリ内の PNG。
  • ノード モジュール
    • fs
    • マイム
    • グロブ
    • 送信

ディレクトリを作成した場所と一致するように、_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
});
4

1 に答える 1

1

添付ファイルの本文を base64 でエンコードする必要があると思います。.dataまた、添付ファイルの内容はではなくに格納されていると思います.body

doc._attachments[_filename].content_type = _mime_type;
doc._attachments[_filename].data = _filecontents.toString('base64');
于 2013-09-22T17:48:30.080 に答える