javascript - How to get multiple images from Pouch -


i have images table need multiple sets of images from. can't figure out easy way this. need loop through .get promise call each image (or set of images).

the following code me 1 image record (which have multiple images in it).

the following code has array each cell of array has string id , list of attachments (separated comma - hence split) have been added image set. s[0] image id , gets sent ".get" image record return , separate each image use else where.

i can't chain don't know how many calls need make , putting calls in loop not way go.

is there way use these id's in promise.all or promise.foreach this?

in example, returning promise or null (been told should promise.resolve(null)). sample starts in ".then" pouch call.

    if (insertedimages != "") {        s = insertedimages[0].split(",");        return db_taskimages.get(s[0], { attachments: true });     }     else {        return;     }  }).then(function (doc) {     if (doc != null) {         itemp = 0; 

thanks.

here section of code working with

     var insertedimages = [];       db_workissues.alldocs({ include_docs: true, descending: false }).then(function (response) {          data = response.rows;         imagektr = 0;          (var = 0; < response.total_rows; i++) {            if (data[i].doc.isattachmentinserted) {               dirtyflag = true;               if (data[i].doc.isattachmentinserted) {                  insertedimages[imagektr++] = data[i].doc._id + "," + data[i].doc.isattachmentinserted;               }               updated[itemp++] = data[i];            }         }          if (updated.length > 0) {            ctl_hiddenjsonworkissues.val(json.stringify(updated));         }          // handle images - handling 1st image record here          if (insertedimages != "") {            s = insertedimages[0].split(",");            return db_taskimages.get(s[0], { attachments: true });         }         else {            return;         }      }).then(function (doc) {         if (doc != null) {             itemp = 0;             (var key in doc._attachments) {               if (itemp > 0) {                  attachments[itemp] = new array();               }               attachments[itemp][0] = key;               attachments[itemp++][1] = doc._attachments[key];            }         }         if (itemp > 0) {            if (attachments[0].length > 0) {               ctl_hiddenjsonimages.val(json.stringify(attachments));            }         }      }).then(function () { 

this handles single image fine, need handle multiples, code gets image changed following .get pass doc (attachment record) thenable follows:

        // insertedimages array of like: "819218,fighterjet.jpg, lions.jpg"          // 819218 key image record , list of images.          var s;          if (insertedimages != "") {            (var = 0; < insertedimages.length;i++) {               s = insertedimages[i].split(",");               return db_taskimages.get(s[0], { attachments: true });            }         } 

i thinking of maybe doing move "s[0]" array , somehow use promise.all thenable part of .all function. not sure how that.

you can use db.alldocs({keys: …}) fetch multiple documents @ once. depending on use case , document structure, might easier create small view emits documents attachments.

below have used db.alldocs. have rewritten code in bit more functional style, might simplify code.

 db_workissues.alldocs({ include_docs: true, descending: false }).then(function (response) {      // need `doc` property each row, map docs functionally.     var docs = response.rows.map(function (row) { return row.doc; });     // filter docs images.     var docswithimages = docs.filter(function (doc) { return doc.isattachmentinserted; });      if (docswithimages.length) {       // don't know does, left here. side effect?       ctl_hiddenjsonworkissues.val(json.stringify(updated));        // depending on how many documents have , how large are,       // might strategy fetch images in multiple batches.       // should use promise.all().       var keys = docswithimages.map(function(doc) { return doc._id; });       return db_taskimages.alldocs({ keys: keys, include_docs: true, attachments: true });     }      // return empty response if there no images.     return {};  }).then(function (imagesresponse) {    var attachmentobjects = imagesresponse.rows      // map rows documents.      .map(function(row) { return row.doc; })      // make sure there documents attachments. shouldn't necessary if flag `isattachmentinserted` consistent _attachments property, let's make sure.       .filter(function (doc) { return !!doc._attachments; })       .map(function (doc) { return doc._attachments });      // extract attachments array. i'd structure easier read , process, function ends there, can't sure.     var attachments = attachmentobjects.reduce(function (list, attachmentobject) {       (var key in attachmentobject) {         list.push([key, attachmentobject[key]);       }       return list;     }, []);     return attachments;   }).then(function () {   … 

just fun, es2016 version, less verbose:

 db_workissues.alldocs({ include_docs: true, descending: false }).then(({rows}) => {    const docswithimages = rows      .map(({doc}) => doc)      .filter(({isattachmentinserted}) => isattachmentinserted);     if (docswithimages.length) {      ctl_hiddenjsonworkissues.val(json.stringify(updated));      return db_taskimages.alldocs({ keys: docswithimages.map(({_id}) => _id), include_docs: true, attachments: true });     }     return {};  }).then(({rows}) =>    rows.map(({doc: {_attachments}) => _attachments)    .filter(att => att)    .reduce((list, attachmentobject) => {      (var key in attachmentobject) {        list.push([key, attachmentobject[key]);      }      return list;    }, [])   }).then(function () {   … 

Comments