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

Categories

0 votes
546 views
in Technique[技术] by (71.8m points)

微信小程序开发工具第一次打开项目无法执行wx.request

image.png
大概就是这种的请求 第一次打开onload能执行 但是success的console没有显示 必须按一下保存重新刷新一下页面才能执行success的console
image.png
我加了一个定时器3秒后执行重新打开就能执行wx.request的success 一秒的话只加载少部分数据 为啥啊 新手 求大佬解释 我知道是异步的问题 但是为什么我刷新一次页面就能正常运行了 网上都找遍了 没有一个靠谱的 用promise也试过 但是只是给最外层的res.request包裹过 可能promise理解不够 才刚学几个月

完整代码

onLoad: function () {

 setTimeout(() => {

 wx.request({

 url: 'https://api.zhuishushenqi.com/ranking/gender',

 method: 'GET',

 success: res => {

 //?console.log(res.data.male)

 //?获的男生畅销榜单的id

 let id = res.data.male[0]._id

 let id2 = res.data.male[5]._id

 //?通过榜单id获取女生畅销榜单书籍

 wx.request({

 url: 'https://api.zhuishushenqi.com/ranking/' + id,

 method: 'GET',

 success: res => {

 //?console.log(res.data.ranking.books)

 let data = res.data.ranking.books;

 let books = [];

 for (let i = 0; i <= 7; i++) {

 let id = data[i]._id

 wx.request({

 url: 'https://api.zhuishushenqi.com/book/' + id,

 method: 'GET',

 success: res => {

 books.push(res.data)

 if (books.length > 7) {

 //?获取男生畅销榜前8本,4本给男生热门,4本给大神区

 let book1 = books.splice(0, 4)

 let book2 = books.splice(0, 4)

 //?console.log(book2)

 //更改data中recommend数组中的books数组

 this.setData({

 ['recommend[0].books']: book1,

 ['recommend[2].books']: book2

 })

 //?console.log(this.data.recommend)

 }

 }

 })

 }

 }

 })

 //?获取完本榜单

 wx.request({

 url: 'https://api.zhuishushenqi.com/ranking/' + id2,

 method: 'GET',

 success: res => {

 let data = res.data.ranking.books

 let books = []

 for (let i = 0; i <= 3; i++) {


 let id = data[i]._id

 wx.request({

 url: 'https://api.zhuishushenqi.com/book/' + id,

 method: 'GET',

 success: res => {

 books.push(res.data)

 if (books.length == 4) {

 this.setData({

 'recommend[1].books': books

 })

 }

 }

 })

 }

 }

 })

 }

 })

 }, 2000);

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

1 Answer

0 votes
by (71.8m points)

把 request 的 fail 回调加上,方便看看报错到底是啥,目前没有报错,不太好排除错误。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...