Am试图在循环中获取数据。我的数据库中有报告,反馈和证据。输入是报告列表,输出将是报告详细信息,与报告相关的反馈列表以及要报告的证据列表。
Sample input :- array of report id [id1,id2,id3]
sample output :- array
{
id1:"reportid",
reportdetail:"reportdetails",
evidences :[e1,
e2,
. .
]
feedback :[f1,f2,f3..]
}
reportArray.forEach(function(singleReport) {
this part of code is not executing because report array is coming empty always.calling this after for loops ends but because of callbacks the value is still not persist here.
});
enter code here
// Get Existing Reports Stored Procedure
function sproc_GetReportsForQueue_Copy(reportObject) {
reportObject = [{
state: "AvailableForT1",
reportId: '295398e7-f468-a541-9d12-70dff0809da3',
id: 20055
},
{
state: "AvailableForT1",
reportId: '00e4a1dc-0b4e-7465-901a-5eaebd50fb41',
id: 20055
}
]
if (!reportObject || !reportObject.length) {
throw "Input parameters are not supplied.";
}
//Initialization
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var reportArray = [];
var mainArray = new Array();
var feedbackArray = new Array();
var evidenceArray = new Array();
for (var i = 0; i < reportObject.length; i++) {
//Gets the state and report id
var state = reportObject[i].state;
var reportId = reportObject[i].reportId;
var reportQuery = "SELECT r.id as reportId,r.routingTargetType as routingTargetType,r.createdTimestamp AS createdTimeStamp from r WHERE r.docType='Report' AND r.id ='" + reportId + "' AND ARRAY_CONTAINS(r.reportState,{state: '" + state + "',isCurrent:true},true)";
var evidenceQuery = "SELECT e.reportId as reportId,e.id As evidenceid,{ moderatorId: e.moderatorNotes.moderatorId, comment: e.moderatorNotes.comment,timeStamp:e.moderatorNotes.timestamp } AS comments,udf.GetEvidenceInfo(e.evidenceDetails) AS evidenceDetail FROM e WHERE e.docType='Evidence' AND e.reportId ='" + reportId + "'";
var feedbackQuery = "SELECT f.reportId as reportId,f.targetGamertag,f.textReason from f WHERE f.reportId ='" + reportId + "' and f.docType='Feedback'";
//Gets the report
var isReportExists = collection.queryDocuments(collection.getSelfLink(), reportQuery,
function(err, documents, options) {
if (err) {
throw err;
}
if (!documents || !documents.length) {
}
if (documents.length > 0) {
reportArray.push(documents);
}
}
);
if (!isReportExists) {
throw "Unable to fetch the reports from the DB";
}
//Gets the Evidence
var isGetEvidence = collection.queryDocuments(
collection.getSelfLink(), evidenceQuery,
function(err, Evidences, options) {
if (err) {
throw err;
}
if (!Evidences || !Evidences.length) {
} else {
evidenceArray.push(Evidences);
}
}
);
if (!isGetEvidence) {
}
var isGetFeedback = collection.queryDocuments(
collection.getSelfLink(), feedbackQuery,
function(err, Feedbacks, options) {
if (err) {
throw err;
}
if (!Feedbacks || !Feedbacks.length) {} else {
feedbackArray.push({
Feedback: Feedbacks
});
}
}
);
}
reportArray.forEach(function(singleReport) {
});
response.setBody();
}
答案 0 :(得分:0)
我稍微简化了您的sql查询,并整理了存储过程。您可以尝试一下。
function sample() {
var reportObject = [{
state: "AvailableForT1",
reportId: '1',
id: 20055
},
{
state: "AvailableForT1",
reportId: '2',
id: 20055
}
]
if (!reportObject || !reportObject.length) {
throw "Input parameters are not supplied.";
}
//Initialization
var collection = getContext().getCollection();
var collectionLink = collection.getSelfLink();
var response = getContext().getResponse();
var reportArray = [];
var mainArray = new Array();
var feedbackArray = new Array();
var evidenceArray = new Array();
for (var i = 0; i < reportObject.length; i++) {
var reportId = reportObject[i].reportId;
getReportInfo(reportId,reportArray)
response.setBody(reportArray);
}
function getReportInfo(reportId,reportArray){
var reportQuery = "SELECT r.id as reportId from r WHERE r.docType='Report' AND r.id ='" + reportId + "'";
var evidenceQuery = "SELECT e.reportId as reportId,e.evidenceId As evidenceid,e.evidenceDetails AS evidenceDetail FROM e WHERE e.docType='Evidence' AND e.reportId ='" + reportId + "'";
var feedbackQuery = "SELECT f.reportId as reportId,f.textReason from f WHERE f.reportId ='" + reportId + "' and f.docType='Feedback'";
//Gets the report
var isReportExists = collection.queryDocuments(collection.getSelfLink(), reportQuery,
function(err, documents, options) {
if (err) {
throw err;
}
if (!documents || !documents.length) {
}
if (documents.length > 0) {
for(var i = 0; i<documents.length;i++){
console.log(documents[i].reportId);
var map = {};
map["id"] = documents[i].reportId;
map["reportDetails"] = documents.reportDetails;
getEvidenceInfo(map,evidenceQuery);
getFeedbackInfo(map,feedbackQuery);
reportArray.push(map);
}
}
}
);
if (!isReportExists) {
throw "Unable to fetch the reports from the DB";
}
}
function getEvidenceInfo(map,evidenceQuery){
//Gets the Evidence
var isGetEvidence = collection.queryDocuments(
collection.getSelfLink(), evidenceQuery,
function(err, Evidences, options) {
if (err) {
throw err;
}
if (!Evidences || !Evidences.length) {
} else {
map["evidences"] = Evidences;
//evidenceArray.push(Evidences);
}
}
);
if (!isGetEvidence) {
throw "Unable to fetch the evidence from the DB";
}
}
function getFeedbackInfo(map,feedbackQuery){
var isGetFeedback = collection.queryDocuments(
collection.getSelfLink(), feedbackQuery,
function(err, Feedbacks, options) {
if (err) {
throw err;
}
if (!Feedbacks || !Feedbacks.length) {} else {
map["Feedback"] = Feedbacks;
// feedbackArray.push({
// Feedback: Feedbacks
// });
}
}
);
if (!isGetFeedback) {
throw "Unable to fetch the feedback from the DB";
}
}
}
我的输出结果:
希望它对您有帮助。