node.js와 nano를 사용하여 CouchDB에 첨부 파일을 대량 업로드하려고합니다. 먼저 보행 모듈은 업로드 폴더의 모든 파일을 찾아서 배열을 생성하는 데 사용됩니다. 다음으로, 배열의 각 파일은 pipe와 nano 모듈을 통해 CouchDB에 삽입된다. 그러나 최종 결과는 하나의 첨부 파일 만 업로드되었음을 나타냅니다.node.js nano 모듈을 사용하여 couchDB에 첨부 파일 대량 업로드
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);
})
)
});
});
});
경우
내가 비동기를 사용하는 것이 좋습니다 예 : – user1276919