문제

I'm trying to bulk upload attachments to CouchDB using node.js and nano. First, the walk module is used to find all files in upload folder and create array from them. Next, each file from the array is supposed to be inserted into CouchDB via pipe and nano module. However, the final result is that only one attachment has been uploaded.

var nano = require('nano')('http://localhost:5984')
var alice = nano.use('alice');
var fs = require('fs');
var walk = require('walk');
var files = [];

// Walker options
var walker = walk.walk('./uploads', {
    followLinks: false
});

// find all files and add to array
walker.on('file', function (root, stat, next) {
    files.push(root + '/' + stat.name);
    next();
});

walker.on('end', function () {
    // files array ["./uploads/2.jpg","./uploads/3.jpg","./uploads/1.jpg"]
    files.forEach(function (file) {
        //extract file name
        fname = file.split("/")[2]

        alice.get('rabbit', {revs_info: true}, function (err, body) {

                fs.createReadStream(file).pipe(

                    alice.attachment.insert('rabbit', fname, null, 'image/jpeg', {
                        rev: body._rev
                    }, function (err, body) {
                        if (!err) console.log(body);
                    })


                )


        });



    });


});
도움이 되었습니까?

해결책

This is because you are mixing an asynchronous api with assumptions of this being synchronous.

After the first request you will get conflicts, cause the rabbit document has changed.

Can you confirm this using NANO_ENV=testing node yourapp.js?

I recommend using async if this is the problem

다른 팁

var nano = require('nano')('http://localhost:5984')
var alice = nano.use('alice');
var fs = require('fs');
var walk = require('walk');
var files = [];

// Walker options
var walker = walk.walk('./uploads', {
    followLinks: false
});

walker.on('file', function (root, stat, next) {
    files.push(root + '/' + stat.name);
    next();
});

walker.on('end', function () {
    series(files.shift());

});



function async(arg, callback) {
    setTimeout(function () {callback(arg); }, 100);
}


function final() {console.log('Done');}


function series(item) {
    if (item) { 
        async(item, function (result) {
            fname = item.split("/")[2]

            alice.get('rabbit', { revs_info: true }, function (err, body) {
                if (!err) {

                    fs.createReadStream(item).pipe(
                    alice.attachment.insert('rabbit', fname, null, 'image/jpeg', {
                        rev: body._rev
                    }, function (err, body) {
                        if (!err) console.log(body);
                    })


                    )

                }
            });

            return series(files.shift());
        });
    } 

    else {
        return final();
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top