微信小程序云开发之增删改查

最近在学习微信小程序的云开发,本想着按照原来的方法学,但是又突然想起来,好多知识学过之后就会在不知不觉中忘了,故产生了写博客记录下所学内容的想法。好,我们直接进入正题。

以下附上学习的链接🔗

http://【【小胖商城】一人一本一天能做到的云开发实战项目 微信小程序云开发系列课程】https://www.bilibili.com/video/BV1W7411p77H?p=9&vd_source=08211467b0aece55c48e430a7dbc4ad8

以下的内容来自链接和笔记

目录

1. 云开发数据的使用之添加

1.1 云端数据库初始化

1.2  云端数据添加的方法

        1.2.1  首先在云控制台数据库里添加一个集合

        1.2.2  获取云端数据集合

 1.3 用云开发的数据库添加方法向云数据新增数据

2. 云开发数据库的使用之删除数据

      2.1 初始化云端数据库

       2.2 删除数据

3.云开发数据库的使用之修改数据

 3.1  修改数据首先需要从主页面跳转到详情,并传递点击这条数据的id

 3.2  在详情页面加载时获取到传值过来的id

3.3 在页面加载函数里获取这个id的数据,并在页面上显示

3.4 修改数据

4. 云开发数据库的使用之获取数据并展示数据

 4.1 初始化云端数据库 + 4.2 获取数据集合

  4.3 获取数据

  4.4 展示数据


1. 云开发数据的使用之添加

          在开始使用数据库API进行增删改查操作之前,需要先获取数据库的引用

1.1 云端数据库初始化

//index.js

const db = wx.cloud.database(); 
//初始化云端数据库 + 获取云端数据的集合

1.2  云端数据添加的方法

        1.2.1  首先在云控制台数据库里添加一个集合

 

 到这里云数据库就创建好了。

        1.2.2  获取云端数据集合

//index.js

const products = db.collection("products"); 
//这里的products是我的数据库的名字,你写的话需要添加上自己刚刚新建数据库的名称。

 1.3 用云开发的数据库添加方法向云数据新增数据

//index.js

onSumnit: function (res) {
add 数据的添加
 products.add({
    data: {
     name: res.detail.value.name, 
     price: res.detail.value.Price,
     dec: res.detail.value.dec
//这里的字段名是自己取的,表示需要向数据库传送的数据
    }
   }).then(res => {
    console.log(res)
   })
}

好了,完事。

为了防止以后看不懂,直接附上原来的代码(跟着学敲的代码)

<!--addProduct.wxml-->

<form bindsubmit="onSumnit">
	Name:<input name= "name" placeholder="请输入产品名称:"></input>
	Price:<input name= "Price" placeholder="请输入价格:"></input>
	Dec:<input name= "dec" placeholder="请输入描述:"></input>
	<button form-type="submit">提交</button>
</form>


// addProduct.wxss
// addProduct.js

//初始化云端数据库 + 获取云端数据的集合
const db = wx.cloud.database();
const products = db.collection("products");


Page({

	data: {

	},

	onSumnit: function (res) {
		if (res.detail.value.name) {
			console.log(res.detail.value)
			//添加数据
			products.add({
				data: {
					name: res.detail.value.name,
					price: res.detail.value.Price,
					dec: res.detail.value.dec
				}
			}).then(res => {
				console.log(res)
			})

		} else {
			wx.showToast({
				title: '请输入添加内容',
				icon: "error",
			})
		}
	},

})

2. 云开发数据库的使用之删除数据

      2.1 初始化云端数据库

//index.js
const db = wx.cloud.database();
const products = db.collection('products');

        2.2 删除数据

	//删除数据
	del:function(){
		products.doc(this.data.product._id).remove().then(res=>{console.log(res)})
	},
//id 是每一个数据存入云数据库自动产生的,可按上面的方法打开数据库擦查看,也可通过函数获得

好的,第二部分收工!

下面是学习时的代码:

<!-- detail.wxml -->
name:			<label>{{product.name}}</label>
price:		<label>{{product.price}}</label>
dec:			<label>{{product.dec}}</label>

<!-- <button bindtap="updata" type="primary">更新数据</button> -->
<button bindtap="del" type="primary">删除数据</button>
/* demo/detail/detail.wxss */
//detail.js
const db = wx.cloud.database();
const products = db.collection('products')

Page({
	data: {
		product: {}
	},
		//更新数据
	updata:function(){
		products.doc(this.data.product._id).update({
			data:{
				price:1040
			},
			success:function(){
				wx.showToast({
					title: '已修改,刷新查看'
				})
			},
			fail:function(res){
				wx.showToast({
					title: '更新失败',
				icon:"error"
				})
				console.log(res)
			},
		})
	},
	//删除数据
	del:function(){
		products.doc(this.data.product._id).remove().then(res=>{console.log(res)})
	},


	onLoad:function(options) {
		console.log(options)
		// 获取得到的id的值
		let id = options.id
		//获取id对应的数据
		products.doc(id).get().then(res => {
			// console.log(res.data)
			this.setData({
				product: res.data
			})
		})
	},
})

3.云开发数据库的使用之修改数据

    3.1  修改数据首先需要从主页面跳转到详情,并传递点击这条数据的id

<!-- index.wxml -->
	<view wx:for="{{products}}" wx:key="_id">
		<navigator url="../detail/detail?id={{item._id}}">
			name:<label>{{item.name}}</label>
			price:<label>{{item.price}}</label>
			dec:<label>{{item.dec}}</label>
		</navigator>
	</view>

  3.2  在详情页面加载时获取到传值过来的id

//detail.js 文件中   刚加载时 获取跳转(index-> detail)页面时,传过来的id

onLoad:function(options) {
		console.log(options)
		// 获取得到的id的值
		let id = options.id
		//获取id对应的数据
		products.doc(id).get().then(res => {
			// console.log(res.data)
			this.setData({
				product: res.data
			})
		})
	},

3.3 在页面加载函数里获取这个id的数据,并在页面上显示

//detail.js
const db = wx.cloud.database();
const products = db.collection('products')

Page({
	data: {
		product: {}
	},
		//更新数据
	updata:function(){
		products.doc(this.data.product._id).update({
			data:{
				price:1040
			},
			success:function(){
				wx.showToast({
					title: '已修改,刷新查看'
				})
			},
			fail:function(res){
				wx.showToast({
					title: '更新失败',
				icon:"error"
				})
				console.log(res)
			},
		})
	},
	//删除数据
	del:function(){
		products.doc(this.data.product._id).remove().then(res=>{console.log(res)})
	},


	onLoad:function(options) {
		console.log(options)
		// 获取得到的id的值
		let id = options.id
		//获取id对应的数据
		products.doc(id).get().then(res => {
			// console.log(res.data)
			this.setData({
				product: res.data
			})
		})
	},
})

 代码在上面,不重复写了。

3.4 修改数据

 

4. 云开发数据库的使用之获取数据并展示数据

 4.1 初始化云端数据库 + 4.2 获取数据集合

//index.js
const db = wx.cloud.database();
const products = db.collection('products');

   4.3 获取数据

//index.js

onLoad(options) {
		products.get().then(res => {
			console.log(res.data)
			this.setData({
				products: res.data
			})
		})
	}

4.4 展示数据

<!-- index.wxml -->
<view>
	<view wx:for="{{products}}" wx:key="_id">
		<navigator url="../detail/detail?id={{item._id}}">
			name:<label>{{item.name}}</label>
			price:<label>{{item.price}}</label>
			dec:<label>{{item.dec}}</label>
		</navigator>
	</view>
</view>

学习代码

<!-- index.wxml -->

<view>
	<view wx:for="{{products}}" wx:key="_id">
		<navigator url="../detail/detail?id={{item._id}}">
			name:<label>{{item.name}}</label>
			price:<label>{{item.price}}</label>
			dec:<label>{{item.dec}}</label>
		</navigator>
	</view>
</view>
view{
	margin-bottom: 20rpx;
}
//初始化数据库
const db = wx.cloud.database();
const products = db.collection('products')


Page({
	data: {
		products: []
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad(options) {
		products.get().then(res => {
			console.log(res.data)
			this.setData({
				products: res.data
			})
		})
	}

})

  • 4
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
微信小程序云开发中,用户可以通过数据库API进行增删改查操作。首先,需要获取数据库的引用\[2\],可以使用以下代码进行初始化: ```javascript const db = wx.cloud.database(); ``` 接下来,需要获取云端数据的集合,可以使用以下代码: ```javascript const products = db.collection("products"); ``` 这里的"products"是数据库的名称,你需要根据自己的数据库名称进行修改\[2\]。 然后,可以使用云开发的数据库添加方法向云数据新增数据。例如,可以在提交表单的事件处理函数中使用以下代码: ```javascript onSubmit: function (res) { products.add({ data: { name: res.detail.value.name, price: res.detail.value.Price, dec: res.detail.value.dec } }).then(res => { console.log(res) }) } ``` 在上述代码中,我们使用了`products.add()`方法向数据库中添加数据。`res.detail.value`表示表单中的值,你可以根据自己的需求修改字段名\[2\]。 除了添加数据,云开发还支持其他的操作,如删除数据、修改数据和查询数据。你可以根据具体需求使用相应的API进行操作。 #### 引用[.reference_title] - *1* *2* *3* [微信小程序云开发增删改查](https://blog.csdn.net/m0_74020066/article/details/129733648)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值