微信雲開發資料庫連線

suN(小硕)發表於2024-07-04
//.js檔案
const db = wx.cloud.database() Page({ //頁面的初始資料 data: { dataObj:"" //定義物件dataObj }, //查詢資料 getData(){ db.collection("pro1").where({ //pro1為資料庫名 author:"張三" //where為條件查詢 }).get() .then(res=>{ //“.then”為成功回撥方法,避免無限迴圈巢狀 this.setData({ //setData函式用於將資料從邏輯層傳送到檢視層(非同步),同時,改變對應的this.data的值(同步) dataObj:res.data }) })
// .catch(err=>{
//   console.log(err) 
// })
//.wxml檔案
<
button type="primary" bind:tap="getData">點選獲取資料</button> //呼叫.js檔案中getData方法
<view wx:for="{{dataObj}}">{{item.title}} - {{item.author}}</view> //動態資料顯示
資料庫內容:

查詢前端頁面情況:



相關文章