0
嗨我想要獲取它在請求中使用請求和cheerio在異步過程中返回的id變量。在異步模塊請求之外獲取變量
我試過通過返回承諾,但沒有奏效。
let id;
request(mainUrl, function(err, res, body) {
let $ = cheerio.load(body)
var links= $(".book").map(function(){
\t \t \t return $(this).attr("href");
\t }).get()
\t \t \t \t \t \t \t
var url= links[0]
var getId= url.match(/book\/show\/(\d+)/)
id= getId[1]
//This is the id I'm getting: "36474"
\t \t \t \t \t \t \t
return Promise.resolve(id)
});
.then((value)=>{
\t console.log(id)
})
什麼要求的lib?看起來像你使用基於回調的,可能不支持'.then' –
*「沒有工作」*不是一個適當的技術問題診斷 – charlietfl