# Using wx-server-sdk in Cloud Function

Cloud functions belong to the management end, and the code running in cloud functions has unrestricted read and write access to databases and cloud files. It is important to note that the runtime environment of a cloud function is the management end, regardless of if the WeChat user corresponding to the incoming openId in the cloud function is the admin or developer of the Mini Program.

The wx-server-sdk dependency should be installed under the corresponding cloud function directory if wx-server-sdk is used in a cloud function. When creating such a function, a new package.json is created by default under the cloud function directory, and you are prompted whether to install the dependency locally. Note that the runtime environment of a cloud function is Node.js, so be sure to install Node.js when installing the dependency locally. In addition, node and npm are both in the environment variables. If there is no need to install the dependency locally, you can use the command line to run it under the directory:

npm install --save wx-server-sdk@latest

As with the Mini Program, before calling other APIs in the cloud function, you must execute the initialization method once:

const cloud = require('wx-server-sdk')
// Default configuration
cloud.init()
// Or pass custom configuration
cloud.init({
  env: 'some-env-id'
})

wx-server-sdk provides database, storage, and cloud function APIs similar to the cloud APIs used by Mini Programs. Several simple examples of database, storage, and cloud function operations are shown below:

# Calling Database in a Cloud Function

Assume there is a todos collection in the database. We can get the data of the todos collection as below:

const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
  // The get method on the collection will return a Promise, so the cloud function will return the result after the database fetches the data asynchronously.
  return db.collection('todos').get()
}

# Calling Storage in a Cloud Function

Assume that we have to upload an image file (demo.jpg) included in the cloud function directory:

const cloud = require('wx-server-sdk')
const fs = require('fs')
const path = require('path')

exports.main = async (event, context) => {
  const fileStream = fs.createReadStream(path.join(__dirname, 'demo.jpg'))
  return await cloud.uploadFile({
    cloudPath: 'demo.jpg',
    fileContent: fileStream,
  })
}

In the cloud function, the value of __dirname indicates the directory in which the cloud function code is located.

# Calling Other Cloud Functions in a Cloud Function

Assume that we have to call another cloud function sum in the cloud function and return the results returned by sum:

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

exports.main = async (event, context) => {
  return await cloud.callFunction({
    name: 'sum',
    data: {
      x: 1,
      y: 2,
    }
  })
}

For more information about wx-server-sdk, refer to the Server API Documentation.

In the next chapter, we will look at the timed trigger, another way to trigger cloud functions.

点击咨询小助手