- 查询到的计数不正确,怎么做?
如图:当在全部项查询的时候,显示正常,切换点击其他项,计数也显示正常。当我在其它项点击查询的时候,显示的计数都是全部项的计数,也就是说,全部项查询的计数是多少,在其他项点击查询的时候显示的都是全部项的计数。如果再切换各项的时候,显示的计数又正常了。请问这个问题怎么解决? [图片] components/segment/wxml代码 <view class="comment-list-group"> <view class="comment-title">全部信息(共有{{result}}条)</view> </view> components/segment/wxml代码 properties: { result :{ type:Number, value:0 }, }, index.wxml代码 <segment-carpool items="{{items}}" result="{{result}}" resultthree="{{result3}}" resultfous="{{result4}}" binditemchengde="onItemChengdeEvent" defaultIndex="0" > <view slot="0" class="segment-page wehicle-page"> <whole wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></whole> </view> <view slot="1" class="segment-page people-page"> <wehicle wx:for="{{listones}}" wx:key="key" wx:for-item="listone" list1="{{listone}}"></wehicle> </view> <view slot="2" class="segment-page train-page"> <people wx:if="{{listtwos}}" wx:for="{{listtwos}}" wx:key="key" wx:for-item="listtwo" list2="{{listtwo}}"></people> </view> <view slot="3" class="segment-page money-page"> <train wx:for="{{listthrees}}" wx:key="key" wx:for-item="listthree" list3="{{listthree}}"></train> </view> <view slot="4" class="segment-page money-page" > <money wx:for="{{listfours}}" wx:key="key" wx:for-item="listfour" list4="{{listfour}}"></money> </view> </segment-carpool> <loadingmore hasmore="{{hasmore}}" nomore="{{nomore}}"></loadingmore> index.js代码 const db = wx.cloud.database(); Page({ data: { items: ["全部", "我要找车", "我要找人", "我找货车", "我要找货"], hasmore: true, nomore: false, wehicles: [], result: 0, startPoint: [], goal: [], isQuerying: false, list:[], listones:[], listtwos:[], listthrees:[], listfours:[], }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, /** * 按条件查询数据 */ formSubmit(event) { console.log(event); const that = this; that.setData({ isQuerying: true }) let list = db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, }); if (event > 0) { list = list.skip(event); } list.limit(10).orderBy("create_time", "desc").get().then(res => { console.log(res); const wehicles = res.data; let nomore = true; let hasmore = true if (!wehicles) { nomore = false } else { hasmore = false } let newWehicles = []; if (event > 0) { newWehicles = that.data.wehicles.concat(wehicles); } else { newWehicles = wehicles; } newWehicles.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }) that.setData({ wehicles: newWehicles, hasmore: hasmore, nomore: nomore, }) }); let list1 = db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, carpool:'我要找车' }); if (event > 0) { list1 = list1.skip(event); } list1.limit(10).orderBy("create_time", "desc").get().then(res => { console.log(res); const listones = res.data; let nomore = true; let hasmore = true if (!listones) { nomore = false } else{ hasmore = false } let newones = []; if (event > 0) { newones = that.data.listones.concat(listones); } else { newones = listones; } newones.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }) that.setData({ listones: newones, hasmore: hasmore, nomore: nomore }) }); let list2 = db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, carpool:'我要找人' }); if (event > 0) { list2 = list2.skip(event); } list2.limit(10).orderBy("create_time", "desc").get().then(res => { console.log(res); const listtwos = res.data; let nomore = true; let hasmore = true if (!listtwos) { nomore = false } else{ hasmore = false } let newtwos = []; if (event > 0) { newtwos = that.data.listtwos.concat(listtwos); } else { newtwos = listtwos; } newtwos.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }) that.setData({ listtwos: newtwos, hasmore: hasmore, nomore: nomore }) }); let list3 = db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, carpool:'我找货车' }); if (event > 0) { list3 = list3.skip(event); } list3.limit(10).orderBy("create_time", "desc").get().then(res => { console.log(res); const listthrees = res.data; let nomore = true; let hasmore = true if (!listthrees) { nomore = false } else{ hasmore = false } let newthrees = []; if (event > 0) { newthrees = that.data.listthrees.concat(listthrees); } else { newthrees = listthrees; } newthrees.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }) that.setData({ listthrees: newthrees, hasmore: hasmore, nomore: nomore }) }); let list4 = db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, carpool:'我要找货' }); if (event > 0) { list4 = list4.skip(event); } list4.limit(10).orderBy("create_time", "desc").get().then(res => { console.log(res); const listfours = res.data; let nomore = true; let hasmore = true if (!listfours) { nomore = false } else{ hasmore = false } let newfours = []; if (event > 0) { newfours = that.data.listfours.concat(listfours); } else { newfours = listfours; } newfours.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }) that.setData({ listfours: newfours, hasmore: hasmore, nomore: nomore, }) }); if(list){ list.count().then(res => { that.setData({ result: res.total, }) }); } else if(list1){ list1.count().then(res => { that.setData({ result: res.total, }) }); } else if(list2){ list2.count().then(res => { that.setData({ result: res.total, }) }); } else if(list3){ list3.count().then(res => { that.setData({ result: res.total, }) }); } else{ list4.count().then(res => { that.setData({ result: res.total, }) }); } }, /** * 初始化总计数 */ oncount(event) { const that = this; db.collection('wehicle').where({}).count().then(res => { that.setData({ result: res.total }) }); }, /** * 获取选项卡计数 */ onItemChengdeEvent(event) { console.log(event) const that = this; var index = event.detail.index; if (index == 0) { if(!that.data.isQuerying){ db.collection('wehicle').where({}).count().then(res => { that.setData({ result: res.total }) }); }else{ db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, }).count().then(res => { that.setData({ result: res.total }) }); } } else if (index == 1) { if(!that.data.isQuerying){ db.collection('wehicle').where({carpool: '我要找车',}).count().then(res => { const result = res.total; console.log(result) that.setData({ result: result, }) }); }else{ db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, carpool:'我要找车' }).count().then(res => { that.setData({ result: res.total }) }); } } else if (index == 2) { if(!that.data.isQuerying){ db.collection('wehicle').where({carpool: '我要找人',}).count().then(res => { const result = res.total; console.log(result) that.setData({ result: result, }) }); }else{ db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, carpool:'我要找人' }).count().then(res => { that.setData({ result: res.total }) }); } } else if (index == 3) { if(!that.data.isQuerying){ db.collection('wehicle').where({carpool: '我找货车',}).count().then(res => { const result = res.total; console.log(result) that.setData({ result: result, }) }); }else{ db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, carpool:'我找货车' }).count().then(res => { that.setData({ result: res.total }) }); } } else { if(!that.data.isQuerying){ db.collection('wehicle').where({carpool: '我要找货',}).count().then(res => { const result = res.total; console.log(result) that.setData({ result: result, }) }); }else{ db.collection("wehicle").where({ startPoint: that.data.startPoint, goal: that.data.goal, carpool:'我要找货车' }).count().then(res => { that.setData({ result: res.total }) }); } } }, })
2024-07-18 - 每次刷新的时候,总是弹出Paused on breakpoint ?
如图,每次编译,或者刷新时就会弹出这个问题,请问这个是什么问题? [图片]
2024-07-18 - 上拉加载,没有数据的情况下一直显示正在加载中,如何解决?
如图,第一次进入页面时,没有数据的情况下应该显示没有更多才对,但是现在是一直显示加载中,除非进行一次上拉动作,才会显示没有更多数据,这个问题怎么解决? [图片] components(WXML代码): <button class="loading-btn" loading="{{hasmore}}" style="height:{{height}}px;"> <block wx:if="{{hasmore}}"> <text class="loadingtext">{{loadingtext}}</text> </block> <block wx:else> <text class="loadingtext" >{{loadedtext}}</text> </block> </button> components(js代码): Component({ /** * 组件的属性列表 */ properties: { loadingtext:{ type:String, value:"正在加载中..." }, loadedtext:{ type:String, value:"没有更多数据" }, hasmore:{ type:Boolean, value:true }, height:{ type:Number, value:40 } }, }) index(wxml代码): <loadingmore hasmore="{{hasmore}}"></loadingmore> index(js代码): data: { hasmore: true, wehicles: [], }, lowdrelease(start = 0) { const that = this; let promise = db.collection("wehicle"); if (start > 0) { promise = promise.skip(start); } promise.limit(10).orderBy("create_time", "desc").get().then(res => { // console.log(res); const wehicles = res.data; let hasmore = true if(wehicles.length == 0 ){ hasmore = false } let newWehicles = []; if (start > 0) { newWehicles = that.data.wehicles.concat(wehicles); } else { newWehicles = wehicles; } // console.log(wehicles); newWehicles.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }) that.setData({ wehicles: newWehicles, hasmore: hasmore }) }) }, onReachBottom() { this.lowdrelease(this.data.wehicles.length); },
2024-07-15 - 查询后的数据 加载完之后,上拉会加载全部项的数据?
问题:如图,通过条件查询后得到是数据,列表中可以正常显示,当查询的数据没有之后,会继续加载全部项的数据,并且查询得到的数据会重新加载一次。 想要的效果:当查询得到的数据加载完之后,应该不再加载数据。显示没有更多数据。如果没有查询操作,正常显示并加载全部项的数据。更多代码可查看代码片段。求大师指点 [图片] WXML代码: <view class="carpool-title"> <text class="title-text">百姓同城拼车网-同城拼车</text> </view> <view class="ad-group"> <image class="ad-image" src="../../images/专注.jpg" mode="" /> </view> <form bindsubmit="formSubmit"> <view class="search-input-group"> <view class="input-group"> <input class="search-input" placeholder="出发地" value="{{startPoint}}" bindinput="onStartPointEvent" /> <image class="thumbnail" src="../../images/return.png" /> <input class="search-input" placeholder="终点" value="{{goal}}" bindinput="onGoalEvent" /> </view> <button class="search-button" size="mini" formType="submit">查询</button> </view> </form> <segment-carpool items="{{items}}" countwe="{{result}}" binditemchengde="onItemChengdeEvent" defaultIndex="0" > <view slot="0" class="segment-page wehicle-page"> <wehicle wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle> </view> <view slot="1" class="segment-page people-page"> <wehicle wx:if="{{wehicle.carpool == '我要找车'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle> </view> <view slot="2" class="segment-page train-page"> <wehicle wx:if="{{wehicle.carpool == '我要找人'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle> </view> <view slot="3" class="segment-page money-page"> <wehicle wx:if="{{wehicle.carpool == '我找货车'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle> </view> <view slot="4" class="segment-page money-page" > <wehicle wx:if="{{wehicle.carpool == '我要找货'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle> </view> </segment-carpool> <loadingmore wx:if="{{wehicles.startPoint && wehicles.goal}}" hasmore="{{hasmore}}"></loadingmore> <loadingmore else hasmore="{{hasmore}}"></loadingmore> JS代码: const db = wx.cloud.database(); const _ = db.command Page({ /** * 页面的初始数据 */ data: { items: ["全部", "我要找车", "我要找人", "我找货车", "我要找货"], hasmore: true, wehicles: [], result: 0, startPoint:'', goal:'', isQuerying:false }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { this.oncount(); }, onStartPointEvent(event){ const that = this; const startPoint = event.detail.value; console.log('起点',startPoint); that.setData({ startPoint:startPoint }) }, onGoalEvent(event){ const that = this; const goal = event.detail.value; console.log('终点',goal); that.setData({ goal:goal }) }, /** * 按条件查询数据 */ formSubmit(start){ const that = this; that.setData({ isQuerying:true }) let promise = db.collection("wehicle").where({ startPoint:that.data.startPoint, goal:that.data.goal, }); if(start > 0){ promise = promise.skip(start); } promise.limit(10).orderBy("create_itme","desc").get().then(res => { const wehicles = res.data; let hasmore = true; if(wehicles.length == 0 || !wehicles){ hasmore = false } let newWehicles = []; if (start > 0) { newWehicles = that.data.wehicles.concat(wehicles); } else { newWehicles = wehicles; } // console.log(wehicles); newWehicles.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }) that.setData({ wehicles: newWehicles, hasmore: hasmore, isQuerying:false }) }) }, oncount() { const that = this; db.collection('wehicle').count().then(res => { that.setData({ result: res.total }) }); }, /** * 获取数据库的总数量 */ onItemChengdeEvent(event) { // console.log(event) const that = this; var index = event.detail.index; let counts = db.collection('wehicle'); const carpool1 = counts.where({carpool: '我要找车',}).count(); const carpool2 = counts.where({carpool: '我要找人',}).count(); const carpool3 = counts.where({carpool: '我找货车',}).count(); const carpool4 = counts.where({carpool: '我要找货',}).count(); let newcarpool = []; if (index == 1) { newcarpool = carpool1.then(res => { const result = res.total; console.log(result) that.setData({ result: result, }) }); } else if (index == 2) { newcarpool = carpool2.then(res => { const result = res.total; console.log(result) that.setData({ result: result, }) }); } else if (index == 3) { newcarpool = carpool3.then(res => { const result = res.total; console.log(result) that.setData({ result: result, }) }); } else if (index == 4){ newcarpool = carpool4.then(res => { const result = res.total; console.log(result) that.setData({ result: result, }) }); }else{ newcarpool = counts.count().then(res=>{ const result = res.total; console.log(result) that.setData({ result: result, }) }) } }, /** * 获取数据库数据 */ lowdrelease(start = 0) { const that = this; let promise = db.collection("wehicle"); if (start > 0) { promise = promise.skip(start); } promise.limit(10).orderBy("create_time", "desc").get().then(res => { // console.log(res); const wehicles = res.data; let hasmore = true; if (wehicles.length == 0 ) { hasmore = false } let newWehicles = []; if (start > 0) { newWehicles = that.data.wehicles.concat(wehicles); } else { newWehicles = wehicles; } // console.log(wehicles); newWehicles.forEach((wehicle, index) => { wehicle.create_time = wehicle.create_time.toString(); }) that.setData({ wehicles: newWehicles, hasmore: hasmore }) }) }, /** * 生命周期函数--监听页面显示 */ onShow() { this.lowdrelease(); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { this.lowdrelease(0); wx.stopPullDownRefresh(); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { // this.formSubmit(this.data.wehicles.length) if(this.data.isQuerying){ this.formSubmit(this.data.wehicles.length) }else{ this.lowdrelease(this.data.wehicles.length); } }, })
2024-07-13 - 下拉刷新,真机上下拉后不回弹,上拉加载,切换页面一直显示加载中?
下拉刷新如图,模拟器正常,真机上不回弹。上拉加载,切换到其他页面时,如果没有数据或者上拉的动作,就一直显示加载中,这问题怎么解决,求助大佬 [图片][图片] wxml代码 <view class="carpool-title"> <text class="title-text">百姓同城拼车网-同城拼车</text> </view> <view class="ad-group"> <image class="ad-image" src="../../images/专注.jpg" mode="" /> </view> <view class="search-input-group"> <view class="input-group"> <input class="search-input" placeholder="出发地" /> <image class="thumbnail" src="../../images/return.png" mode="" /> <input class="search-input" placeholder="终点" /> </view> <button class="search-button" size="mini">查询</button> </view> <segment-carpool items="{{items}}" wehicle="{{wehicle}}" binditemchengde="onItemChengdeEvent" defaultIndex="0"> <view slot="0" class="segment-page wehicle-page"> <wehicle wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle> </view> <view slot="1" class="segment-page people-page"> <wehicle wx:if="{{wehicle.carpool == '我要找车'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle> </view> <view slot="2" class="segment-page train-page"> <wehicle wx:if="{{wehicle.carpool == '我要找人'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle> </view> <view slot="3" class="segment-page money-page"> <wehicle wx:if="{{wehicle.carpool == '我找货车'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle> </view> <view slot="4" class="segment-page money-page"> <wehicle wx:if="{{wehicle.carpool == '我要找货'}}" wx:for="{{wehicles}}" wx:key="wehicle" wx:for-item="wehicle" wehicle="{{wehicle}}"></wehicle> </view> </segment-carpool> <loadingmore hasmore="{{hasmore}}"></loadingmore> JS代码 const db = wx.cloud.database(); Page({ data: { items:["全部","我要找车","我要找人","我找货车","我要找货"], hasmore:true, wehicles:[], }, onLoad(options) { }, onItemChengdeEvent(event){ }, lowdrelease(start=0){ const that = this; let promise = db.collection("wehicle"); if(start>0){ promise = promise.skip(start); } promise.limit(10).orderBy("create_time", "desc").get().then(res => { const wehicles = res.data; let hasmore = true; if(wehicles.length == 0){ hasmore = false } let newWehicles = []; if(start > 0){ newWehicles = that.data.wehicles.concat(wehicles); }else{ newWehicles = wehicles; } that.setData({ wehicles:newWehicles, hasmore:hasmore }) }) }, /** * 生命周期函数--监听页面显示 */ onShow() { this.lowdrelease(); wx.stopPullDownRefresh(); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { this.lowdrelease(0); }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { this.lowdrelease(this.data.wehicles.length); }, })
2024-07-02 - 多个组件排一行换行问题?
如第一张图,换行后就另起一行了,希望效果能够像第二张图一样,依次排列后换行,求指教 [图片][图片] <view class="wehicle-top"> <text class="top">顶</text> <text class="start">{{wehicle.startPoint}}</text> <text class="arrow">→</text> <text class="end">{{wehicle.goal}}</text> <text wx:if="{{wehicle.carpool == '我要找人'}}" class="pass">({{wehicle.pass}}但是沃尔沃温热热私人而色色是二色)</text> <!--途径--> </view> WXSS代码: .wehicle-top{ display:flex; justify-content:flex-start; flex-flow:wrap; } .wehicle-top .top{ margin-right:10rpx; background-color:red; padding:0 10rpx; border-radius:10rpx; font-size:28rpx; color:#fff; } .wehicle-top .start{ font-size:32rpx; font-weight:700; margin-right:10rpx; } .wehicle-top .arrow{ font-size:32rpx; font-weight:700; margin-right:10rpx; } .wehicle-top .end{ font-size:32rpx; font-weight:700; margin-right:10rpx; } .wehicle-top .pass{ font-size:28rpx; font-weight:700; }
2024-06-30 - 如何把components组件中的picker值保存到数据库中?
在components组件中定义了picker,在pages页面中,如何把picker中的值保存到数据库中? 图片 [图片][图片] 代码: components中: selector代码: <view class="section"> <picker bindchange="bindPickerChange" value="{{index}}" range="{{array}}"> <view class="section-picker"> <view class="picker"> {{arrays}} </view> <icon class="icon"></icon> </view> </picker> </view> js代码: Component({ /** * 组件的属性列表 */ properties: { array:{ type:Array, value:[] }, arrays:{ type:String, value:'' } }, /** * 组件的初始数据 */ data: { index:0 }, /** * 组件的方法列表 */ methods: { bindPickerChange(event){ console.log('picker发送选择改变,携带值为', event.detail.value) var that = this; var index = event.detail.value; var array = that.properties.array; that.setData({ arrays:array[index], // index: index }) var detail = {'index':index}; var options = {}; that.triggerEvent('carporate',detail,options) } } }) pickerday代码: <picker mode="date" fields="day" value="{{date}}" start="{{start}}" end="{{end}}" bindchange="onbindDataChange"> <view class="section-picker"> <view class="picker"> {{date}} </view> <icon class="icon"></icon> </view> </picker> js代码: var util = require('../../utils/util.js'); var minday = util.formatTime(new Date()).split(' ')[0]; var maxday = util.formatTime(new Date((new Date()).getTime()+(1000*60*60*24*62))).split(' ')[0]; Component({ /** * 组件的属性列表 */ properties: { date:{ type:String, value:'' } }, /** * 组件的初始数据 */ data: { // date:today, start: minday, end:maxday, }, /** * 组件的方法列表 */ methods: { onbindDataChange(event){ console.log(event); var value = event.detail.value; this.setData({ date:value }) }, } }) pages页面代码: <form bindsubmit="onReleaseEvent"> <view class="carpool-group"> <view class="carpool-title">拼车类型</view> <selector name="carpool" array="{{carpool}}" arrays="{{carpools}}" bindcarporate="onCarporateEvent"></selector> </view> <view class="carpool-group"> <view class="carpool-title">出发日期</view> <pickerday name="day" date="{{day}}"></pickerday> </view> <view class="carpool-group"> <view class="carpool-title">出发时间</view> <pickertime name="times" time="{{times}}"></pickertime> </view> <button class="btn" formType="submit">提交</button> </form> js代码: const db = wx.cloud.database() const app = getApp(); Page({ /** * 页面的初始数据 */ data: { carpool:['我要找车','我要找人','我找货车','我要找货'], carpools:'请选择拼车类型', day:'请选择出发日期', times:'请选择出发时间' }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { }, onCarporateEvent(event){ console.log(event); var that = this; var index = event.detail.index; that.setData({ index:index }) }, /** * 发布信息 */ onReleaseEvent(event){ console.log(event); const that = this; const carpool = that.data.carpool[0]; // 如何获取拼车类型 const day = that.data.day; // 如何获取出发日期 const times = that.data.times; // 然后获取出发时间 const start = event.detail.value.start; const goal = event.detail.value.goal; const car = event.detail.value.car; const peopleNumber = event.detail.value.peopleNumber; const phone = event.detail.value.phone; const content = event.detail.value.content; const author = app.globalData.userInfo; db.collection("wehicle").add({ data:{ carpool:carpool, day:day, times:times, start:start, goal:goal, car:car, peopleNumber:peopleNumber, phone:phone, content:content, author:author } }) }, })
2024-06-29 - 用户授权,获取app.globalData.userInfo为undefined?
查了很多资料说是异步问题,也做了修改,但还是为undefined。要怎么修改?求帮助、 app.js打印的图片 [图片] login.js打印的图片 [图片] // app.js App({ onLaunch:async function () { if (!wx.cloud) { console.error('请使用 2.2.3 或以上的基础库以使用云能力'); } else { wx.cloud.init({ env: '1g4z5k3lc74cf9e2', traceUser: true, }); } this.globalData = {}; await this.loadUserInfo(); console.log('loadUserInfo有没有执行',this.loadUserInfo); console.log('有没有获取到userInfo',this.globalData.userInfo); }, async loadUserInfo() { var that = this; let loginOpenid = await wx.cloud.callFunction({ name:'logins', }) console.log('获取用户openid',loginOpenid); that.globalData.openid = loginOpenid.result.openid; console.log('保存openid',that.globalData.openid); const loginRes = await wx.cloud.database().collection('login') .where({ _openid:'loginOpenid' }).get() console.log('查询是否有用户信息',loginRes); that.globalData.userInfo = loginRes.data[0]; console.log('保存用户信息到globalData',that.globalData.userInfo); }, is_login() { console.log('is_logn获取',this.globalData.userInfo); if (this.globalData.userInfo) { return true } else { return false } }, }) login.js代码 // pages/login/login.js const app = getApp(); const db = wx.cloud.database(); Page({ data: { }, onLoad(options) { }, // 获取头像 getAvatar(event) { console.log('临时头像路劲地址',event.detail.avatarUrl); //临时头像路劲地址 let avatarUrl = event.detail.avatarUrl; //临时地址 let suffix = /\.[^\.]+$/.exec(avatarUrl)[0]; wx.cloud.uploadFile({ cloudPath: 'loginImages/' + new Date().getTime() + suffix, filePath: avatarUrl //上传到云端的路劲, }).then(res => { console.log('头像路劲地址',res.fileID); this.setData({ avatarUrl: res.fileID }) }) }, async login(event) { console.log(event); let nickName = await event.detail.value.nickName; console.log('用户名', nickName); if(!nickName){ wx.showToast({ title:'请填写昵称', icon:'error' }) return } if(!this.data.avatarUrl){ wx.showToast({ title:'请上传头像', icon:'error' }) return } let openid =await app.globalData.openid; console.log("获取app.globalData.openid",openid); try{ let existingUserQuery = await db.collection('login').where({ _openid:openid }).get(); console.log('获取用户信息',existingUserQuery) if(existingUserQuery.data.length > 0){ this.setData({ userInfo:existiongUserQuery.data[0], }); }else{ let newUser = { num:Date.now(), avatarUrl:this.data.avatarUrl, nickName:nickName, }; await db.collection('login').add({ data:newUser }); } }catch(err){ console.log('Error',err); } wx.showToast({ title:'登陆成功' }); setTimeout(()=>{ wx.navigateBack({}); },1500) }, }) user-center.js代码 const app = getApp(); console.log('是否有app信息',app) const db = wx.cloud.database(); Page({ data: { }, onLoad:async function(options) { console.log('是否有传递数据',app.is_login()); if(app.is_login()){ this.setData({ userInfo: app.globalData.userInfo, avatarUrl: app.globalData.userInfo.avatarUrl, nickName: app.globalData.userInfo.nickName }) } onuserInfo(e){ wx.navigateTo({ url:'/pages/login/login' }) },
2024-06-08 - globalData打印出来是undefined?
写了段授权代码,设置全局配置时,is_login()中this.globalData.userInfo为undefined,在页面pages.user-center打印app.globalData.userInfo就是undefined,打印app.is_login()就是false,在pages.login页面中打印app.is_login()时为true,也就能够正常,打印app.globalData.userInfo又能够正常打印出来,请问这个问题怎么解决? 如图: [图片] app.js代码 App({ onLaunch: function () { if (!wx.cloud) { console.error('请使用 2.2.3 或以上的基础库以使用云能力'); } else { wx.cloud.init({ env: '', traceUser: true, }); } this.globalData = {}; this.loadUserInfo(); }, loadUserInfo() { //获取用户的openid var that = this; wx.cloud.callFunction({ name: 'login', success(res) { // console.log(res); that.globalData.openid = res.result.openid //查找数据库用户表里面是否有这个用户记录 wx.cloud.database().collection('login').where({ _openid: res.result.openid }).get({ success(result) { console.log(result) // that.globalData.userInfo = result.data[0] if(result.data[0]){ wx.setStorageSync('login',result.data[0]) // that.globalData.userInfo = wx.getStorageSync('userInfo') that.globalData.userInfo = result.data[0] console.log(that.globalData.userInfo); // 这里能够正常打印出来数据 } } }) } }) }, is_login() { console.log(this.globalData.userInfo); // 这里打印为undefined if (this.globalData.userInfo) { return true } else { return false } }, 云函数代码: const cloud = require('wx-server-sdk') cloud.init() const db = cloud.database(); // 云函数入口函数 exports.main = async (event, context) => { const wxContext = cloud.getWXContext() return { event, openid: wxContext.OPENID, appid: wxContext.APPID, unionid: wxContext.UNIONID, } } pages.login页面代码: const app =getApp(); const db = wx.cloud.database(); Page({ data: { }, onLoad(options) { }, // 获取头像 getAvatar(event){ console.log(event.detail.avatarUrl);//临时头像路劲地址 let avatarUrl = event.detail.avatarUrl;//临时地址 let suffix = /\.[^\.]+$/.exec(avatarUrl)[0]; wx.cloud.uploadFile({ cloudPath: 'loginImages/' + new Date().getTime() + suffix, filePath:avatarUrl //上传到云端的路劲, }).then(res => { console.log(res.fileID); this.setData({ avatarUrl:res.fileID }) }) }, login(event){ console.log(event.detail.value.nickName); let nickName = event.detail.value.nickName; if(!nickName){ wx.showToast({ title:'请填写昵称', icon:'error' }) return } if(!this.data.avatarUrl){ wx.showToast({ title:'请上传头像', icon:'error' }) return } var that = this; console.log(app.is_login()); // 在这里又能够正常打印出来 db.collection('login').where({ _openid:app.globalData.openid }).get({ success(res){ console.log(res) if(res.data.length == 0){ //添加记录到数据库 // var avatarUrl = that.data.avatarUrl; db.collection('login').add({ data:{ num:Date.now(),//!QQ号 avatarUrl:that.data.avatarUrl, nickName:nickName }, success(res){ console.log(res) wx.showToast({ title:'登陆成功', }) setTimeout(function(){ wx.navigateBack({}) },1500) } }) }else{ that.setData({ userInfo:res.data[0] }) } } }) },
2024-06-02 - 在页面中globalData为null或者false?
在模拟器中,如果把我的页面放在前面展示,登陆成功后返回,页面没有获取到,如图一,如果首页放在前面展示,再打开我的页面就能够正常获取到,网上查了些资料,说是异步问题,没有处理过这种问题,代码改怎么写? [图片][图片] APP代码: App({ onLaunch: function () { if (!wx.cloud) { console.error('请使用 2.2.3 或以上的基础库以使用云能力'); } else { wx.cloud.init({ env:'', traceUser: true, }); } this.globalData = {}; this.loadUserInfo(); }, loadUserInfo(){ //获取用户的openid var that = this; wx.cloud.callFunction({ name:'login', success(res){ // console.log(res); that.globalData.openid = res.result.openid //查找数据库用户表里面是否有这个用户记录 wx.cloud.database().collection('login').where({ _openid:res.result.openid }).get({ success:result => { // console.log(result) that.globalData.userInfo = result.data[0] } }) } }) }, is_login(){ if(this.globalData.userInfo){ return true }else{ return false } }, }); wxml代码: <view class="userinfo-group"> <!-- 已登陆 --> <view class="user-group" wx:if="{{userInfo != null}}"> <image class="avatra" src="{{avatarUrl?avatarUrl:'../../images/avatar.png'}}"></image> <view class="right-group"> <view class="nickname">用户名:{{nickName}}</view> <view class="ID">用户ID:dfd9765490</view> </view> </view> <!-- 未登陆 --> <block wx:if="{{userInfo == null}}"> <view class="user-group" bind:tap="onuserInfo"> <image class="avatra" src="../../images/avatar.png"></image> <view class="right-group"> <view class="nickname">点击登录</view> <view class="ID">登录获取更多权限</view> </view> </view> </block> </view> JS代码: Page({ data: { }, onLoad(options) { console.log(app.is_login()); if(app.is_login()){ this.setData({ userInfo:app.globalData.userInfo, avatarUrl:app.globalData.userInfo.avatarUrl, nickName:app.globalData.userInfo.nickName }) } }, onuserInfo(){ wx.navigateTo({ url:'/pages/login/login' }) }, 代码片段 https://developers.weixin.qq.com/s/A59C3ym57VRA
2024-05-29