Record the message function of doing cloud development recently, including the message of the user, and the message list function of the administrator, the administrator has not read and read functions

First of all, this first user message function, message including message content, upload pictures, mobile phone number, email

Add functions to the cloud database

Add there are two methods, one is directly in JS to add to the database, one is through the cloud function to add to the database

Add cloud functions to the database

The JS part of the page

let form = { content: this.data.content, email: this.data.email, phone: this.data.phone, imgname: this.data.imagePath? This.data. imagePath: ", readState: 1, randnum: this.rand(1000,9999), nickName: info.nickname} wx.showLoading({title: 'submitting ',}); wx.cloud .callFunction({ name: 'feedback', data: { ... form, }, }) .then(() => { wx.hideLoading(); This.closetoptip = $wuxToptips().success({hidden: false, text: 'Your feedback has been received and will be handled as soon as possible! ', duration: 150000, success: () => { this.closeToptip = null; } }) this.setData({ imagePath: '', content: '', email: '', phone: '' }); }) .catch(() => { wx.hideLoading(); Wx.showtoast ({title: 'submit failed ', icon: 'success', duration: 2000,}); });Copy the code

Cloud feedback function

const cloud = require('wx-server-sdk'); cloud.init(); const db = cloud.database(); function format(date) { return date.getFullYear() + '-' + parseZero(date.getMonth() + 1) + '-' + parseZero(date.getDate()) + ' ' + parseZero(date.getHours()) + ':' + parseZero(date.getMinutes()) + ':' + parseZero(date.getSeconds()) } function parseZero(num) { return num < 10 ? '0' + num : num; } exports.main = async event => { try { const {OPENID} = cloud.getWXContext(); Console. log('OPENID',OPENID) // Store user subscription information in cloud development database const result = await db.collection('messages').add({data: {... event, touser: OPENID, cretime: format(new Date(new Date().getTime() + 8 * 60 * 60 * 1000)) }, }); return result; } catch (err) { console.log(err); return err; }};Copy the code

Js inside add to the database

let form = { content: this.data.content, email: this.data.email, phone: this.data.phone, imgname: this.data.imagePath? This.data. imagePath: '', readState: 1, randnum: this.rand(1000,9999), nickName: info.nickName } const db = wx.cloud.database(); */ db. Collection ('messages').add({data:{... form } }) .then(res=>{ console.log(res) })Copy the code

Cloud database query function

Query is divided into query all, with conditional query (DOC and WHERE) two conditional statements

If the database is not created by itself, you must change the database permissions to be readable by all users, or you cannot find the data

Query all data

Db.collection ('messages').get({// If the query is successful, success: If (res.data.length>0) {this.setData({conList:) {console.log('messages', res) // This step is very important. res.data.reverse() }) } }, fail: err => { console.log('err', err) } })Copy the code

Query conditional doc

If you use doc, you can only use the “_id” value of the cloud database, click on the “_id” parameter from the page,

Db.collection ('messages').doc('_id ').get({// If the query is successful, success: If (res.data.length>0) {this.setData({conList:) {console.log('messages', res) // This step is very important. res.data.reverse() }) } }, fail: err => { console.log('err', err) } })Copy the code

Query where with conditions

If you use WHERE, you can query multiple fields

Db.collection ('messages'). Where ({content: this.data.content, email: this.data.email,}).get({// If the query is successful, success: If (res.data.length>0) {this.setData({conList:) {console.log('messages', res) // This step is very important. res.data.reverse() }) } }, fail: err => { console.log('err', err) } })Copy the code

Cloud database modification function (doc or WHERE can be used to query data before updating)

If you use doc, you can only use “_id “, otherwise you can’t find the data.

db.collection('Evaluation').doc('_openid').update({
    data:{
        dislike:_.unshift('dishName')
    },
    success(res) {
        console.log(res.data)
    }
})
Copy the code

Update method is used in the cloud data modification function. If the database is not created by yourself, the _openID field will not be generated automatically. The system considers that you are not the creator and cannot update the database

If you want to add the _openID field to your database, add the _openID field to your database. If you want to add the _openID field to your database, add the _openID field to your database. Open the cloud development console and select the data permission configuration of the operation data table from the console as shown in the figure below:

Note the last custom security rule and change it to the following by clicking on the right:

{
  "read": true,
  "write": true
}
Copy the code

Cloud database deletion function

Remove method is used in cloud data deletion function. If doc is used, only “_id value “can be used, otherwise the data cannot be found.

const db = wx.cloud.database(); */ db.collection('messages').doc('XIjKR97E7L4wsc48').remove().then(res=>{console.log(res)})Copy the code

conclusion

Thank you for sharing articles and videos. Thank you for your sharing. It is because of you that I know that while sharing technology, I am also constantly summarizing my achievements during this period of time and making myself better and better.

Finally read the friends feel helpful words, give me a thumbs-up, thank you!!