Joke Collection Website - Blessing messages - How to use cloud function to realize praise function in applet cloud development tutorial

How to use cloud function to realize praise function in applet cloud development tutorial

In this article, we will use the cloud function on Tall to realize the like function.

What is a cloud function?

Cloud functions Cloud functions are functions that run in the cloud (server side). In physical design, a cloud function can be composed of multiple files, occupying a certain amount of CPU memory and other computing resources; Each cloud function is completely independent; Can be deployed in different areas. ? When the applet calls the cloud function, the defined code will be executed in the Node.js runtime environment.

There are actually examples of cloud functions in new projects for reference.

First, we right-click the cloudfunctions folder, create a new nodeJs cloud function, and then name it vote. Click Enter, and it will pop up later to tell us the environment for installing the cloud function, but only if you have installed the node environment and npm.

To install node, you can go directly to node official website, download an msi file and install it all the way.

After installing node, npm is actually installed by default.

Click OK, and after the installation is completed, we click to close the command line window.

Next, add:

Voting: Function (e){

var arr = this . data . votearr;

var id = Number(e . current target . dataset . index),

d = this . data . datas;

console.log(id)

if (arr.indexOf(D[id].id)! = - 1){

D[id]。 vote-= 1;

arr.splice(arr.indexOf(D[id])。 id), 1)

this.setData({

Data: d,

Vauters: Ah.

})

} Otherwise {

arr.push(D[id].id)

this.setData({

Vauters: Ah.

})

if (id || id == 0) {

D[id]。 zaurl = this . data . zanicon 1

D[id]。 vote = Number(D[id]。 Voting)+1

}

let data = {

Voting: number (D[id]. Voting)+1,

id: D[id]。 id,

userId:wx . getstoragesync(' userId '),

}

Console.log (data)

wx.cloud.callFunction({

Name,' Voting',

//Parameters required for image:

//Like+1

//id of this article

Data: {

Voting: number (D[id]. Voting)+1,

id: D[id]。 id,

},

Success: res =>{

wx.showToast({

Title: "like success",

})

this.setData({

: d data company

})

},

Failure: err =>{

wx.showToast({

Icon: "None",

Title: "Like failure",

})

Console.error('[ cloud function] call failed:', err)

}

})

}

},

Next, we write index.js under the cloud function vote:

//cloud function entry file

const cloud = require(' wx-server-SDK ')

cloud.init()

const db = cloud.database()

//cloud function entry function

Exports.main = async (event, context) =>{

//To get the passed parameters, you can use {vote, id} = event more succinctly.

var vote = event.vote,id = event.id

Console.log ('Cloud zan succeeded', voting, id)

// console.warn (data)

Try {

return await db . collection(' funnys ')。 Among them ({

Id: number (id)

}). Update ({

Data: {

Voting: Voting

},

Success: res =>{

Console.log ('Cloud function succeeded', voting, id)

},

Failure: e =>{

Console. Error (e)

}

})

} catch (e) {

Console. Error (e)

}

}

After writing, we right-click the voting folder and click the upload cloud function. After uploading, we can test it.

Click like, and we find that the picture will turn into a yellow like icon, instead of using the background database, this.setData will be used instead.

Then the cloud function will be called to vote. If it is successful, there will be a toast to praise the success. If it fails, the console will also prompt related errors.

Then, so far, the like function is basically completed. Please see: detailed code.