# My First Cloud Function

Let's define a function that adds two numbers as our first cloud function example.

Find the project.config.json file in the root directory of the project, add the cloudfunctionRoot field, and then specify an existing local directory as the local root directory of the cloud function.

Example:

{
   "cloudfunctionRoot": "./functions/"
}

Refer to the documentation for additional configurations of project.config.json.

After you specify the directory, the icon of root directory of the cloud function will become the "icon of cloud directory", and the first level directory (cloud function directory) under the root directory of cloud function is same as the name of cloud function. If there is such cloud function in the corresponding online environment, a special "cloud icon" is used to indicate it.

Next, right-click the root directory of the cloud function. In the right-click menu, select to create a new Node.js cloud function named "add". The Developer Tools creates the cloud function directory and the entry index.js file locally and creates the corresponding cloud function in the online environment. After successful creation, the Tools will prompt whether to install the local dependency immediately. Upon confirmation, the Tools automatically installs wx-server-sdk. We can see a cloud function template similar to the one below:

const cloud = require('wx-server-sdk')
// Entry function of the cloud function
exports.main = async (event, context) => {

}

The cloud function has two incoming parameters: the event object and the context object. event refers to the event that triggers the cloud function. When the Mini Program calls the cloud function, it passes the event parameter. In addition, the openid of the Mini Program user and the appid of the Mini Program are injected automatically by the backend. The context object contains the call information and operation status of the call, which can be used to understand the operating status of the service. wx-server-sdk is required in the template by default. This is a library provided by WeChat to allow us to work with databases in the cloud function, store data, and call other cloud functions. The use of wx-server-sdk will be discussed in another chapter.

Fill in the template:

exports.main = async (event, context) => {
  return {
    sum: event.a + event.b
  }
}

The code in this section add the incoming a and b parameters and return the result to the caller as a sum field.

Before calling the cloud function in the Mini Program, we should deploy the cloud function to the cloud. Right-click the cloud function directory. In the right-click menu, we can package the cloud function as a whole and then upload it to the online environment.

After the deployment, we can call the cloud function in the Mini Program:

wx.cloud.callFunction({
  // Name of cloud function
  name: 'add',
  // The parameters to be transmitted to the cloud function
  data: {
    a: 1,
    b: 2,
  },
  success: function(res) {
    console.log(res.result.sum) // 3
  },
  fail: console.error
})

Promise-styled calls are also supported:

wx.cloud.callFunction({
  // Name of cloud function
  name: 'add',
  // The parameters to be transmitted to the cloud function
  data: {
    a: 1,
    b: 2,
  },
})
.then(res => {
  console.log(res.result) // 3
})
.catch(console.error)

We have now successfully created our first cloud function and called it in the Mini Program.

In the next chapter, we will introduce how cloud functions seamlessly integrate with Mini Program login status and how Mini Program user information (openid and appid) can be obtained from the cloud function.