将 1 个同步变量和 1 个异步变量传递给下一个异步函数
Pass 1 sync and 1 async variables to next async function
我正在使用 mongodb 具有简单自定义查询功能的领域 return 数据库数据
function mongo(data) {
return fetch(
apiurl,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}
)
.then((response) => {
return response.json();
})
.then((data) => {
console.log("Success:", data);
return data;
})
.catch((error) => {
console.error("Error:", error);
});
}
mongo({
collection: "metadata",
action: "find",
criteria: { query: {}},
}).then((metadata) => {
for (var i = 0; i < metadata.length; i++) {
var oneMeta= metadata[i];
var subjectName = oneMeta.name;
var subjectCode = oneMeta.code;
var blockHTML =
`<div>
<span class="badge badge-danger">` + subjectName + `</span>
<span class="badge badge-dark">` + subjectCode + `</span>
</div>`;
var oneSubject = document.createElement("div");
oneSubject.id = "sub_"+subjectCode
oneSubject.innerHTML = blockHTML;
subjectOutput.appendChild(oneSubject);
}
});
我正在尝试为每个metadata
做另一个mongo({collection: "actualData", action: "count", criteria: { query: {subjectId: XXX}}})
查询,其中XXX
将是 subjectCode
来自 metadata[i]
目标是使用其 id 将另一个 <span>count</span>
附加到上面创建的 DIV,其中计数是第二个 mongo
函数的结果。
我只能在没有循环的情况下做到这一点 metadata.length
我需要在循环中每次执行下一个函数,同时使用 i
和 metadata
感谢任何帮助!
只需将您的 metadata
映射到一个 mongo
承诺的数组,await
所有承诺,然后计数结果将在数组的相同位置:
mongo({
collection: "metadata",
action: "find",
criteria: { query: {}},
}).then(async (metadata) => {
const counts = await Promise.all(metadata.map((oneMeta) => {
return mongo({collection: "actualData", action: "count", criteria: { query: {subjectId: oneMeta.subjectCode}}});
}));
for (let i = 0; i < metadata.length; i++) {
const count = counts[i];
const oneMeta = metadata[i];
const subjectName = oneMeta.name;
const subjectCode = oneMeta.code;
var blockHTML =
`<div>
<span class="badge badge-danger">` + subjectName + `</span>
<span class="badge badge-dark">` + subjectCode + `</span>
<span class="badge badge-dark">` + count + `</span>
</div>`;
var oneSubject = document.createElement("div");
oneSubject.id = "sub_"+subjectCode
oneSubject.innerHTML = blockHTML;
subjectOutput.appendChild(oneSubject);
}
})
我正在使用 mongodb 具有简单自定义查询功能的领域 return 数据库数据
function mongo(data) {
return fetch(
apiurl,
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}
)
.then((response) => {
return response.json();
})
.then((data) => {
console.log("Success:", data);
return data;
})
.catch((error) => {
console.error("Error:", error);
});
}
mongo({
collection: "metadata",
action: "find",
criteria: { query: {}},
}).then((metadata) => {
for (var i = 0; i < metadata.length; i++) {
var oneMeta= metadata[i];
var subjectName = oneMeta.name;
var subjectCode = oneMeta.code;
var blockHTML =
`<div>
<span class="badge badge-danger">` + subjectName + `</span>
<span class="badge badge-dark">` + subjectCode + `</span>
</div>`;
var oneSubject = document.createElement("div");
oneSubject.id = "sub_"+subjectCode
oneSubject.innerHTML = blockHTML;
subjectOutput.appendChild(oneSubject);
}
});
我正在尝试为每个metadata
做另一个mongo({collection: "actualData", action: "count", criteria: { query: {subjectId: XXX}}})
查询,其中XXX
将是 subjectCode
来自 metadata[i]
目标是使用其 id 将另一个 <span>count</span>
附加到上面创建的 DIV,其中计数是第二个 mongo
函数的结果。
我只能在没有循环的情况下做到这一点 metadata.length
我需要在循环中每次执行下一个函数,同时使用 i
和 metadata
感谢任何帮助!
只需将您的 metadata
映射到一个 mongo
承诺的数组,await
所有承诺,然后计数结果将在数组的相同位置:
mongo({
collection: "metadata",
action: "find",
criteria: { query: {}},
}).then(async (metadata) => {
const counts = await Promise.all(metadata.map((oneMeta) => {
return mongo({collection: "actualData", action: "count", criteria: { query: {subjectId: oneMeta.subjectCode}}});
}));
for (let i = 0; i < metadata.length; i++) {
const count = counts[i];
const oneMeta = metadata[i];
const subjectName = oneMeta.name;
const subjectCode = oneMeta.code;
var blockHTML =
`<div>
<span class="badge badge-danger">` + subjectName + `</span>
<span class="badge badge-dark">` + subjectCode + `</span>
<span class="badge badge-dark">` + count + `</span>
</div>`;
var oneSubject = document.createElement("div");
oneSubject.id = "sub_"+subjectCode
oneSubject.innerHTML = blockHTML;
subjectOutput.appendChild(oneSubject);
}
})