Wechat Small program Cloud Development (I)
Create a project
The steps are not described one by one. The following points should be noted:
Cloud development projects must have appIDS and test numbers cannot be used
I did not find the entry where the test number can be used
Open the wechat developer tool to create the environment
If the project cannot be associated with the service space after the successful creation, restart the wechat developer tool to solve the problem
Cloud functions are correlated
Download the wX-server-SDK dependencies before uploading cloud functions (if used in index.js)
npm install --save wx-server-sdk@latest
Copy the code
Note that this is at the root of the cloud function, not the project root
Import the Vant UI framework
See the official Vant documentation for more details
Create a homepage page as the homepage of the homepage
The style is as follows. The main function is to record the English new words I want to record
Create the edit page Edit as follows
The main function is to edit my new words
Main Functions of the project
- The query
- add
- delete
Create a collection
Creating a cloud function
- Cloudfunctions file right-click to create a new NodeJS cloudfunction
- Go to the words root directory to download the WX-server-SDK dependencies
npm install --save wx-server-sdk@latest
Copy the code
Operating database Problems
Wechat applet provides two ways to operate the database
- Applets operate directly on the database
- Cloud functions operate databases
To compare
In cases where other users can alter data created by others, it is recommended to put database operations in the cloud. (Generally speaking, this kind of situation is more, it is recommended to put directly in the cloud)
Cloud functions are used here to manipulate the database
Considering that there are three scenarios: query, add and delete, I uniformly operate in words cloud function
- The applet passes a type to tell the cloud function what to do,
const db = cloud.database()
const {
type
} = event
Copy the code
Note that this is cloud.database(), not wx.
Policy pattern differentiation
const rule = {
'query':async() = > {// Query the service logic
},
'add':async() = > {// Add business logic
},
'delete':async() = > {// Delete the business logic}},return rule[type]()
Copy the code
Note here that all three callbacks are asynchronous async and require a return rule[type]() because the cloud function returns an asynchronous result
Cloud function query
const {
pageSize,
pageNum
} = event
return await db.collection('words').orderBy('createTime'.'desc').skip((pageNum - 1) * pageSize).limit(pageSize).get().then(res= > {
console.log(res.data)
return res.data
})
Copy the code
Cloud function added
const {
word,
wordExplain
} = event
return await db.collection('words').add({
data: {
word: word,
wordExplain: wordExplain,
createTime: db.serverDate() // Add the field
},
}).then(res= > {
return {
code: 200}})Copy the code
Cloud function deletion
Const {id,} = event console.log(' here ', id) return await db.collection('words'). Where ({_id: id }).remove().then(res => { return { code: 200 } })Copy the code
Notice the return await here