第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

發(fā)出多個(gè)api / http node.js請(qǐng)求很困難

發(fā)出多個(gè)api / http node.js請(qǐng)求很困難

慕妹3242003 2021-04-27 13:26:45
您好,我有兩個(gè)api請(qǐng)求可以單獨(dú)工作,但是要讓它們都在我的node.js應(yīng)用程序上工作都遇到了很多麻煩。有沒(méi)有簡(jiǎn)單的方法可以發(fā)出兩個(gè)請(qǐng)求?我嘗試過(guò)提出兩個(gè)請(qǐng)求,將數(shù)據(jù)放入變量中,然后呈現(xiàn)它們兩個(gè),但是遇到全局變量問(wèn)題。任何幫助,將不勝感激。  request(url, function (err, response, body) {    if(err){      res.render('index', {weather: null, error: 'Error, please try again'});    } else {      let weather = JSON.parse(body);      if(weather.main == undefined){        res.render('index', {weather: null, error: 'Error, please try again'});      } else {        if (rain == "0,rain"){        let weatherText = `It's ${weather.main.temp} degrees celsius and with wind speeds of ${weather.wind.speed} mph in ${weather.name} ${weather.sys.country}! & ${weather.weather[0].description}`  ;        res.render('index', {weather: weatherText, error: null});      }      else{        let weatherText = `It's ${weather.main.temp} degrees celsius and with wind speeds of ${weather.wind.speed} mph in ${weather.name} ${weather.sys.country}!`  ;        res.render('index', {weather: weatherText, error: null});      }      }    }  });request(url2, function (err, response, body) {  if(err){    res.render('index', {news: null, error: 'Error, please try again'});  } else {    let result = JSON.parse(body);    let news = result.articles[0].title    if(news == undefined){      res.render('index', {news: null, error: 'Error, please try again'});    }     else{      res.render('index', {news:news, error: null});    }    }  })
查看完整描述

2 回答

?
烙印99

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊

我會(huì)使用“承諾”來(lái)等待,直到收到您的第一個(gè)請(qǐng)求的回復(fù)為止。一旦發(fā)生這種情況,您可以觸發(fā)第二個(gè)請(qǐng)求(請(qǐng)參閱下面的鏈接中的“ then”)。一旦第二個(gè)請(qǐng)求返回結(jié)果,就可以在使用數(shù)據(jù)之前合并兩個(gè)請(qǐng)求的響應(yīng)。

如果請(qǐng)求B需要使用來(lái)自請(qǐng)求A響應(yīng)的數(shù)據(jù),則請(qǐng)求A應(yīng)該位于鏈中的第一位。這樣,您可以根據(jù)第一個(gè)請(qǐng)求的響應(yīng)(例如,使用IF語(yǔ)句)來(lái)調(diào)整第二個(gè)請(qǐng)求。如果兩者完全獨(dú)立,則可以選擇其中一個(gè)請(qǐng)求先行。

在此鏈接上有關(guān)于鏈接承諾的部分,該部分應(yīng)與您要執(zhí)行的操作有關(guān):https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/然后

有關(guān)使用promise和'catch'處理錯(cuò)誤的詳細(xì)信息https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Promise/catch


查看完整回答
反對(duì) 回復(fù) 2021-05-13
?
倚天杖

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超3個(gè)贊

盡管確實(shí)可以簡(jiǎn)化請(qǐng)求結(jié)構(gòu),但實(shí)際上沒(méi)有必要使用promise,但是您可以輕松地嵌套請(qǐng)求:


request(url, function(err, response, body) {

    request(url2, function(err2, response2, body2) {

        // Both request data is available here

    })

})

請(qǐng)注意,可以在此處更改回調(diào)函數(shù)中使用的變量名稱


如果您有一堆可以獨(dú)立運(yùn)行的請(qǐng)求,請(qǐng)調(diào)查 promise.all


查看完整回答
反對(duì) 回復(fù) 2021-05-13
  • 2 回答
  • 0 關(guān)注
  • 231 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)