我有一个图像表,我需要从中获取多组图像。我无法找到一种简单的方法来做到这一点。我需要为每个图像(或图像集)循环.get promise调用。
以下代码将为我提供一张图像记录(其中可能包含多个图像)。
以下代码有一个数组,其中数组的每个单元格都有一个字符串,其中包含ID和附加列表(用逗号分隔 - 因此分割)已添加到此图像集。 s [0]是图像ID,是发送给" .get"获取我返回的图像记录并将每个图像分开以用于其他地方。
我无法将其链接起来,因为我不知道需要拨打多少电话,并且将电话置于循环中显然不是可行的方法。
有没有办法在Promise.all或Promise.foreach中使用这些ID来做到这一点?
在示例中,我返回一个promise或null(被告知我应该做一个Promise.resolve(null))。样本开始于"然后"从另一个小袋电话。
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 insertedImages = [];
DB_WorkIssues.allDocs({ include_docs: true, descending: false }).then(function (response) {
data = response.rows;
imageKtr = 0;
for (var i = 0; i < 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 - only handling the 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;
for (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 () {
这可以很好地处理单个图像,但我需要它来处理倍数,其中获取图像的代码将被更改为执行如下操作,其中.get将doc(附件记录)传递给thenable如下:
// insertedImages would be an array of something like: "819218,fighterjet.jpg, lions.jpg"
// where 819218 is the key for the image record and then a list of images.
var s;
if (insertedImages != "") {
for (var i = 0; i < insertedImages.length;i++) {
s = insertedImages[i].split(",");
return DB_TaskImages.get(s[0], { attachments: true });
}
}
我在考虑做些什么我会移动所有的&#34; s [0]&#34;进入一个数组并以某种方式使用Promise.all,然后thenable将成为.all函数的一部分。只是不知道该怎么做。
答案 0 :(得分:0)
您可以使用db.allDocs({keys: …})
一次获取多个文档。根据您的使用案例和文档结构,创建一个仅发出带附件的文档的小视图可能更容易。
下面我使用了db.allDocs。我已经用更多功能的样式重写了你的代码,这可能会简化代码。
DB_WorkIssues.allDocs({ include_docs: true, descending: false }).then(function (response) {
// We only need the `doc` property from each row, so we map the docs functionally.
var docs = response.rows.map(function (row) { return row.doc; });
// Filter to docs with images.
var docsWithImages = docs.filter(function (doc) { return doc.IsAttachmentInserted; });
if (docsWithImages.length) {
// I don't know what this does, so I left it here. Side effect?
CTL_HiddenJsonWorkIssues.val(JSON.stringify(updated));
// Depending on how many documents you have and how large they are,
// it might be a good strategy to fetch the images in multiple batches.
// Then you 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 an empty response if there are no images.
return {};
}).then(function (imagesResponse) {
var attachmentObjects = imagesResponse.rows
// Map the rows to the documents.
.map(function(row) { return row.doc; })
// Make sure there are only documents with attachments. This shouldn't be necessary if the flag `IsAttachmentInserted` is always consistent with the _attachments property, but let's make extra sure.
.filter(function (doc) { return !!doc._attachments; })
.map(function (doc) { return doc._attachments });
// Extract the attachments as array. I'd say that another structure would be easier to read and process, but as your function ends there, I can't say for sure.
var attachments = attachmentObjects.reduce(function (list, attachmentObject) {
for (var key in attachmentObject) {
list.push([key, attachmentObject[key]);
}
return list;
}, []);
return attachments;
}).then(function () {
…
只是为了好玩,这是ES2016版本,它不那么冗长:
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) => {
for (var key in attachmentObject) {
list.push([key, attachmentObject[key]);
}
return list;
}, [])
}).then(function () {
…