-1

I am trying to insert a media document to cloudant couchdb using below below code.

var uuid = require('uuid').v4;

fs.readFile('./test.jpeg', function(err, data) {
    if (!err) {
        var newAttachmentObj = { 
            type: 'media',
            media_mime: 'image/jpeg',
            media_type: 'Photo',
            media_filename: 'rabbit1'
        }

        var filename = 'rabbit1';
        var media_mime =  'image/jpeg';
        var attachment_id = uuid();
        var media_data = data;
        console.log(data);
        console.log(newAttachmentObj);
        console.log(attachment_id);

        db.multipart.insert(newAttachmentObj,
            [{ name: filename, data: media_data, content_type: media_mime }], attachment_id, function (err, body) {
            console.log(body);
            if (err) {
                console.log('Error: Creating a media doc in cloudant.');
                console.log(err);
                // console.log(JSON.stringify(err));
            } else {
                console.log(body);
            }
        })
    }
});

Document is getting created in the cloudant couchdb and also we can view the uploaded attachment, but the callback function returns an error as shown below.

{ Error: function_clause
  at Request._callback (/home/boatman/anoop/forwarding-module/node_modules/cloudant-nano/lib/nano.js:248:15)
  at Request.self.callback (/home/boatman/anoop/forwarding-module/node_modules/request/request.js:188:22)
  at emitTwo (events.js:125:13)
  at Request.emit (events.js:213:7)
  at Request.<anonymous> (/home/boatman/anoop/forwarding-module/node_modules/request/request.js:1171:10)
  at emitOne (events.js:115:13)
  at Request.emit (events.js:210:7)
  at IncomingMessage.<anonymous> (/home/boatman/anoop/forwarding-module/node_modules/request/request.js:1091:12)
  at Object.onceWrapper (events.js:314:30)
  at emitNone (events.js:110:20)
  at IncomingMessage.emit (events.js:207:7)
  at endReadableNT (_stream_readable.js:1045:12)
  at _combinedTickCallback (internal/process/next_tick.js:138:11)
  at process._tickDomainCallback (internal/process/next_tick.js:218:9)
    name: 'Error',
    error: 'unknown_error',
    reason: 'function_clause',
    ref: 944644368,
    scope: 'couch',
    statusCode: 500,
    request: 
    { method: 'PUT',
    headers: { 'content-type': 'multipart/related' },
    uri: 'https://XXXXXX:XXXXXX@account_id-bluemix.cloudant.com/db_media/a73d3788-d661-4944-964b-bcffce0286bd',
    multipart: [ [Object], [Object] ] },
    headers: 
    { 'cache-control': 'must-revalidate',
    'content-type': 'application/json',
    date: 'Tue, 17 Apr 2018 08:37:28 GMT',
    'x-couch-request-id': '5097b3e876',
    'x-couch-stack-hash': '944644368',
    'x-frame-options': 'DENY',
    'strict-transport-security': 'max-age=31536000',
    'x-content-type-options': 'nosniff',
    'x-cloudant-request-class': 'write',
    'x-cloudant-backend': 'bm-cc-uk-04',
    via: '1.1 lb1.bm-cc-uk-04 (Glum/1.50.4)',
    statusCode: 500,
    uri: 'https://XXXXXX:XXXXXX@account_id-bluemix.cloudant.com/db_media/a73d3788-d661-4944-964b-bcffce0286bd' },
    errid: 'non_200',
    description: 'couch returned 500' }

Please find the image below. Image trying to attaach

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
AnoopGoudar
  • 914
  • 9
  • 18

1 Answers1

1

I tried to reproduce your conditions with CouchDB 2.1.1, not Cloudant, therefore it may not be an exact reproduction, but I thought I'd share my results.


I created the following server.js file based on your code with a little modification. I used package nano for CouchDB not cloudant-nano since I'm not using Cloudant:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; // Ignore rejection, becasue CouchDB SSL certificate is self-signed
var uuid = require('uuid').v4;
const fs = require('fs')
const nano = require('nano')('https://admin:****@192.168.1.106:6984');
// Database 'reproduce' is already created in CouchDB 2.1.1
const db = nano.db.use('reproduce');

fs.readFile('./test.jpeg', function(err, data) {
    if(err){
        console.log('err -> ', err)
    }else if (!err) {
        var newAttachmentObj = {
            type: 'media',
            media_mime: 'image/jpeg',
            media_type: 'Photo',
            media_filename: 'rabbit1'
        }

        var filename = 'rabbit1';
        var media_mime =  'image/jpeg';
        var attachment_id = uuid();
        var media_data = data;
        console.log(data);
        console.log(newAttachmentObj);
        console.log(attachment_id);

        db.multipart.insert(newAttachmentObj,
            [{ name: filename, data: media_data, content_type: media_mime }], attachment_id, function (err, body) {
                console.log(body);
                if (err) {
                    console.log('Error: Creating a media doc in cloudant.');
                    console.log(err);
                    // console.log(JSON.stringify(err));
                } else {
                    console.log(body);
                }
            })
    }
});

I placed a sample test.jpeg in the same directory as my server.js file. Then I ran the code with command $ node server.js and I got the following results:

$ node server.js 
<Buffer ff d8 ff e1 00 32 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 01 00 98 82 02 00 0e 00 00 00 1a 00 00 00 00 00 00 00 52 61 77 70 69 78 65 6c 20 4c 74 64 ... >
{ type: 'media',
  media_mime: 'image/jpeg',
  media_type: 'Photo',
  media_filename: 'rabbit1' }
ec6a36d1-952e-4d86-9865-3587c6079fb5
{ ok: true,
  id: 'ec6a36d1-952e-4d86-9865-3587c6079fb5',
  rev: '1-896eca9e9980509aeaa8539b281c3257' }
{ ok: true,
  id: 'ec6a36d1-952e-4d86-9865-3587c6079fb5',
  rev: '1-896eca9e9980509aeaa8539b281c3257' }

Obviously, I don't receive the errors your getting.

Megidd
  • 7,089
  • 6
  • 65
  • 142