- watch怎么监听不了数据?
const app = getApp() var musicwatcher = null Page({ /** * 页面的初始数据 */ data: { }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { const db = wx.cloud.database() this.musicwatcher = db.collection('notify').where({ notifyType: 1, notifyOpenid: 'oo7SZ5A-B8MS6WWJzsZ2RF8DXEio', }).watch({ onChange: function (snapshot) { if (snapshot.docChanges.length) { app.globalData.visitorNumber = snapshot.docChanges.length } }, onError: function (err) { console.error('the watch closed because of error', err) } }) } 是我的写法有什么问题吗?
2024-03-16 - app.js的 globalData怎么在tabber的首页获取不到数据,在其他页面就能获取到?
// app.js App({ globalData: { userInfo: null, visitorNotify: { visitorExist: false, visitorNumber: '' } }, async checkLogin() { const db = wx.cloud.database() const user = await wx.cloud.callFunction({ name: 'loginCheck', data: {} }) const re = await db.collection('user').where({ openid: user.result.openid }).get().then(res => { this.userInfo = res.data[0] this.globalData.userInfo = res.data[0]; }) }, async onLaunch() { wx.cloud.init({ env: 'green-7gnsotdye1b374a3', traceUser: true }) await this.checkLogin(); await this.notifyPlus(); }, async notifyPlus() { const db = wx.cloud.database() await db.collection('notify').where({ notifyType: 1, notifyOpenid: this.globalData.userInfo.openid, }).watch({ onChange: (snapshot) => { if (snapshot.docChanges.length) { console.log(snapshot.docChanges); this.globalData.visitorNotify.visitorExist=true this.globalData.visitorNotify.visitorNumber=snapshot.docChanges.length } }, onError: (err) => { console.error('the watch closed because of error', err); } }) console.log('登录了') } }) [图片] 这是tabber页面的首页面, [图片] 这是tabber的其他页面,这个页面可以获取到globalData的数据,首页的tabber就不行,这是为什么?
2024-03-15