微信云开发数据库连接

//.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>   //动态数据显示
数据库内容:

 查询前端页面情况:

 

 



 

posted @ 2024-07-04 16:16  suN(小硕)  阅读(3)  评论(0编辑  收藏  举报