Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am fetching match id and series id from my firebase database and storing it in object called ids and pushing ids values into an array called arr.(我正在从我的Firebase数据库中获取匹配ID和系列ID,并将其存储在名为ID的对象中,并将ID值推入名为arr的数组中。)

var options3 = { method: "GET", hostname: "dev132-cricket-live-scores-v1.p.rapidapi.com", port: null, path: "/match.php?seriesid=SID&matchid=MID", headers: { "x-rapidapi-host": "dev132-cricket-live-scores-v1.p.rapidapi.com", "x-rapidapi-key": "63e55e4f7fmsh8711fb1c0bd9ec2p1d8b4bjsne2b8db0a1a82" } }; function makeStatusLive(data) { var arr = []; console.log("m aa gya", data[1]); let upcomingMatchRef = db .collection("Match_Creator") .doc("cricket") .collection("matchList"); let queryUpcomingMatch = upcomingMatchRef .where("status", "==", "upcoming") .get() .then(snapshot => { if (snapshot.empty) { console.log("No matching documents."); return; } snapshot.forEach(doc => { // getting matchId and seriesId from the database and storing it in "ids" object let ids = { matchId: "", seriesId: "" }; ids.matchId = doc.data().matchId; ids.seriesId = doc.data().seriesId; arr.push(ids); console.log(arr); // console.log(ids); }); }) Now i am sending this ids values to 3rd party api to check status of the matches and return me the status values of respective match ids(现在,我将此ID值发送给第三方api,以检查匹配状态并向我返回相应匹配ID的状态值) .then(() => { // passing "ids" value into options3 path to check for status of these particular matches from the api for (i = 0; i <= arr.length; i++) { var urlJson = options3.path; var matchid = arr[0].matchId; var seriesid = arr[0].seriesId; urlJson = urlJson.replace(/SID/g, seriesid); //replacing SID(in options3 path) with seriesId of "ids" object. urlJson = urlJson.replace(/MID/g, matchid); //replacing MID(in options3 path) with matchId of "ids" object. options3.path = urlJson; // final options3 path after passing "ids" values console.log(urlJson); var req = http.request(options3, res => { var chunks = []; var arr = []; res.on("data", chunk => { chunks.push(chunk); }); console.log("aman", i); res.on("end", () => { var body = Buffer.concat(chunks); var json = JSON.parse(body); const statusMatch = json.match.status; console.log(statusMatch); }); }); req.end(); } //end of for loop }) .catch(err => { console.log("Error getting documents", err); }); } Output what i am getting:(输出我得到的是:) functions: Beginning execution of "storeMatchData" > m aa gya COMPLETED > [ { matchId: '46202', seriesId: '2385' } ] > [ { matchId: '46202', seriesId: '2385' }, > { matchId: '45879', seriesId: '2345' } ] > [ { matchId: '46202', seriesId: '2385' }, > { matchId: '45879', seriesId: '2345' }, > { matchId: '46117', seriesId: '2376' } ] > [ { matchId: '46202', seriesId: '2385' }, > { matchId: '45879', seriesId: '2345' }, > { matchId: '46117', seriesId: '2376' }, > { matchId: '46290', seriesId: '2402' } ] > /match.php?seriesid=2385&matchid=46202 > /match.php?seriesid=2385&matchid=46202 > /match.php?seriesid=2385&matchid=46202 > /match.php?seriesid=2385&matchid=46202 > Error getting documents TypeError: Cannot read property 'matchId' of undefined > at upcomingMatchRef.where.get.then.then (C:UsersAKASHDesktoppredicta-functionfunctionsindex.js:149:30) > at process._tickCallback (internal/process/next_tick.js:68:7) > aman 4 > INPROGRESS > aman 4 > INPROGRESS > aman 4 > INPROGRESS > aman 4 > INPROGRESS its returning the status of first matchid only for every match id.(仅针对每个匹配ID返回其第一匹配ID的状态。) I want status of each and every match id to be returned.(我想返回每个比赛ID的状态。)   ask by Aman Singh translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
404 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try to create a dynamic Promise chaining:(尝试创建动态的Promise链接:)

.then(() => { var getStatusMatch = function(item, i) { return new Promise(resolve => { var urlJson = options3.path; var matchid = item.matchId; var seriesid = item.seriesId; urlJson = urlJson.replace(/SID/g, seriesid); //replacing SID(in options3 path) with seriesId of "ids" object. urlJson = urlJson.replace(/MID/g, matchid); //replacing MID(in options3 path) with matchId of "ids" object. options3.path = urlJson; // final options3 path after passing "ids" values console.log(urlJson); var req = http.request(options3, res => { var chunks = []; res.on("data", chunk => { chunks.push(chunk); }); console.log("aman", i); res.on("end", () => { var body = Buffer.concat(chunks); var json = JSON.parse(body); const statusMatch = json.match.status; console.log(statusMatch); resolve(statusMatch); }); }); req.end(); }); }; function downloadChain(arr) { return arr.reduce((chain, item, index) => { return chain.then(() => getStatusMatch(item, index)); }, Promise.resolve()); } return downloadChain(arr); }).catch(err => { console.log("Error getting documents", err); });

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...