微信小程序云开发-数据库查询渲染

1、集合(表)创建

建议创建表时最好带上“_openid”字段
在这里插入图片描述
也可以json导入和表格导入

//json格式
{"name":"大神",_openid:"",,"num":0.0,"content":"内容"},
{"name":"大神2",_openid:"","num":0.0,"content":"内容"},
{"name":"大神3",_openid:"","num":0.0,"content":"内容"},
{"name":"大神4",_openid:"","num":0.0,"content":"内容"},
{"name":"大神5",_openid:"","num":0.0,"content":"内容"},
{"name":"大神6",_openid:"","num":0.0,"content":"内容"},
{"name":"大神7",_openid:"","num":0.0,"content":"内容"},
{"name":"大神8",_openid:"","num":0.0,"content":"内容"},
{"name":"大神9",_openid:"","num":0.0,"content":"内容"},
{"name":"大神10",_openid:"","num":0.0,"content":"内容"},
{"name":"大神11",_openid:"","num":0.0,"content":"内容"}

2、权限设置

在这里插入图片描述

3、查询接口

页面的index.js文件

//index.js
const app = getApp()

Page({
  data: {
    formData:[   //定义页面渲染变量
    {_id: "ko", _openid: "", content: "内容", name: "大神", num: 0},
    {_id: "ko", _openid: "", content: "内容", name: "大神", num: 0}
    ]
  },
  onLoad: function() {
    const db = wx.cloud.database();
    db.collection('formData').where({
      _openid: ""     //查询条件
    }).get().then(res => {//formData是数据库里面集合的名称
      console.log(res); //如果更新数据成功则输出成功信息
      var that = this;
      that.setData({
        formData: res.data  //数据赋值
      });
      console.log(res.data);
    }).catch(err => {
      console.log(err); //如果更新数据失败则输出失败信息
    })
  },
})

4、页面数据渲染

页面index.wxml文件

<!--index.wxml-->
<view class="page-body">
	<view class="btn-area" id="buttonContainer">
	
		<view wx:for='{{formData}}' wx:key='index'>
			<text>{{item.name}}\n</text>
		</view>
		
	</view>
</view>

在这里插入图片描述
更多内容可参考:https://www.w3cschool.cn/weixinapp/weixinapp-1giq2zgj.html

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GY程序源

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值