2024-08-27 10:08:47 +08:00

49 lines
890 B
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 云数据库
首先第一步,需要初始化云服务器
你需要拿到你的环境ID接下来需要在 app.js 中做初始化工作
```js
App({
onLaunch() {
// 初始化云服务
wx.cloud.init({
env: 'cloud1-5gsobkys7eb1b3ef'
});
},
})
```
初始化完毕后,我们就可以使用云服务了(云数据库、云存储、云函数)
下面是在云数据库中增加数据的示例:
```js
// 获取云端的数据库实例
const db = wx.cloud.database();
```
首先获取云端数据库的实例,接下来通过数据库实例获取集合
```js
// 再从数据库中获取到集合(表)
const students = db.collection('students');
```
通过集合调用相应的方法来进行增删改查,例如要增加一条记录,那就是调用 add 方法
```js
students.add({
data : this.data
}).then(res=>{
console.log(res)
})
```