I. premise preparation
1. Open wechat web developer tool;
2. Create a project and select cloud development;
3. Click cloud development side by side with simulator, editor and compiler to open cloud development console;
4. Click to open for the first time, and create an environment after opening;
5. Select the cloudfunctions folder, right-click to select more settings, and then select environment;
6. Select the login folder under cloudfunctions;
7. Right click to select upload and deployment: cloud installation dependency (do not upload node "moudules);
8. Create a test set in the database of the cloud development console;
9. Create a main directory under miniprogram / pages /;
10. Create a main Page in the main directory;
II. Entering development
- Use main as home page
In miniprogram/pages/app.json, move "pages/main/main" to the front.
{
"pages": [
"pages/main/main",
"pages/index/index",
"pages/userConsole/userConsole",
"pages/storageConsole/storageConsole",
"pages/databaseGuide/databaseGuide",
"pages/addFunction/addFunction",
"pages/deployFunctions/deployFunctions",
"pages/chooseLib/chooseLib",
"pages/openapi/openapi"
],
...
- main.wxml page layout
<view>
<input style='margin-top: 40rpx;' placeholder="Please enter a name" value="{{name}}" bindinput="bindKeyInputName" />
<input style='margin-top: 40rpx;' placeholder="Please enter age" value="{{age}}" bindinput="bindKeyInputAge" />
<button style='margin-top: 40rpx;' bindtap='insertData'>insert data</button>
<input style='margin-top: 40rpx;' placeholder="Please enter a record ID" value="{{recordId}}" bindinput="bindKeyInputId" />
<button style='margin-top: 40rpx;' bindtap='queryData'>Query data</button>
<text style='margin-top: 40rpx;'>
//Name: {{nameResult}}
</text>
<text style='margin-top: 80rpx;'>
//Age: {{ageResult}}
</text>
</view>
- main.js processing business logic
const app = getApp()
Page({
db: undefined,
test: undefined,
data: {
name: '',
age: '',
recordId: '',
nameResult: '',
ageResult: ''
},
onLoad: function (options) {
var that = this
wx.cloud.callFunction({
name: 'login',
data: {},
success: res => {
console.log('[Cloud function] [login] user openid: ', res.result.openid)
app.globalData.openid = res.result.openid
wx.cloud.init({ env: 'yale' })
that.db = wx.cloud.database()
that.test = that.db.collection('test')
},
fail: err => {
console.error('[Cloud function] [login] Call failed', err)
wx.navigateTo({
url: '../deployFunctions/deployFunctions',
})
}
})
},
insertData: function () {
var that = this
try {
var age = parseInt(that.data.age)
if (isNaN(age)) {
wx.showModal({
title: 'error',
content: 'Please enter the correct age',
showCancel: false
})
return
}
this.test.add({
data: {
name: that.data.name,
age: age
},
success: function (res) {
console.log(res)
wx.showModal({
title: 'Success',
content: 'Record inserted successfully',
showCancel: false
})
that.setData({
name: '',
age: ''
})
}
})
}
catch (e) {
wx.showModal({
title: 'error',
content: e.message,
showCancel: false
})
}
},
queryData: function () {
var that = this
this.db.collection('test').doc(this.data.recordId).get({
success: function (res) {
that.setData({
nameResult: res.data.name,
ageResult: res.data.age
})
},
fail: function (res) {
wx.showModal({
title: 'error',
content: 'record was not found',
showCancel: false
})
}
})
},
bindKeyInputName: function (e) {
this.setData({
name: e.detail.value
})
},
bindKeyInputAge: function (e) {
this.setData({
age: e.detail.value
})
},
bindKeyInputId: function (e) {
this.setData({
recordId: e.detail.value
})
},
})
- test
1. Enter the name and age on the main page, click insert data, and then view the data just added in the cloud control platform;
2. Copy the id of the data from the cloud control platform, paste it into the input box on the query data button, and then click the query button to display the data found in the cloud database below.