官方文档解读:
<!-- 查询记录 --> <view class="guide" wx:if="{{step === 4}}"> <text class="headline">查询记录</text> <text class="p">1. 打开 pages/databaseGuide/databaseGuide.js 文件,定位到 onQuery 方法</text> <text class="p">2. 把注释掉的代码解除注释,onQuery 方法会在下方按钮被点击时触发</text> <image class="code-image" src="../../images/code-db-onQuery.png" mode="aspectFit"></image> <text class="p">3. 点击按钮</text> <button size="mini" type="default" bindtap="onQuery">查询记录</button> <text class="code" wx:if="{{queryResult}}">{{queryResult}}</text> <div class="nav"> <button class="prev" size="mini" type="default" bindtap="prevStep">上一步</button> <button class="next" size="mini" type="default" bindtap="nextStep">下一步</button> </div> </view>
wxml:
<button size="mini" type="default" bindtap="onQuery">查询记录</button> <text class="code" wx:if="{{queryResult}}">{{queryResult}}</text>
js:
// pages/databaseGuide/databaseGuide.js const app = getApp() Page({ data: { step: 1, counterId: '', openid: '', count: null, queryResult: '', }, onLoad: function(options) { if (app.globalData.openid) { this.setData({ openid: app.globalData.openid }) } }, onQuery: function() { const db = wx.cloud.database() // 查询当前用户所有的 counters db.collection('counters').where({ _openid: this.data.openid }).get({ success: res => { this.setData({ queryResult: JSON.stringify(res.data, null, 2) }) console.log('[数据库] [查询记录] 成功: ', res) }, fail: err => { wx.showToast({ icon: 'none', title: '查询记录失败' }) console.error('[数据库] [查询记录] 失败:', err) } }) }, })