May 20, 2021 WeChat Mini Program Development Document
Before you start using cloud capabilities on the smaller side, you need to call the wx.cloud.init method to initialize cloud capabilities (note that the small program needs to start the cloud service by clicking the "console" button in the upper left corner of the toolbar). Therefore, if we want to use cloud capabilities, we usually call this method when the small program is initialized.
The wx.cloud.init method is defined as follows:
function init(options): void
The wx.cloud.init method accepts an optional options argument, and the method does not return a value.
The options parameter defines the default configuration for cloud development, which serves as the default configuration for all other cloud APIs that are called later, and options provide the following optional configuration:
Field | The data type | Required | The default | Description |
---|---|---|---|---|
Env | string | object | Whether | default | The default environment configuration, the environment ID in the form of an incoming string, can specify the default environment for all services, and the incoming object can specify the default environment for each service separately, as detailed below |
traceUser | boolean | Whether | false | Whether or not user access is logged to user management and is visible in the console |
When env is passed in as an object, you can specify the default environment for each service, with the following optional fields:
Field | The data type | Required | The default | Description |
---|---|---|---|---|
database | string | Whether | default | The database API default environment configuration |
storage | string | Whether | default | The storage API default environment configuration |
functions | string | Whether | default | Cloud function API default environment configuration |
Sample code:
wx.cloud.init({
env: 'test-x1dzi'
})
The CLOUD.INIT method is defined as follows:
function init(options): void
Cloud.init method accepts an optional options parameter that does not return a value.The method can only call once, and only the first call takes effect multiple times.
The Options parameter defines the default configuration of the cloud development. This configuration will then call the default configuration of all other cloud APIs, and options provided by Options are as follows:
Field | type of data | Required | Defaults | illustrate |
---|---|---|---|---|
env | string | object | Yes |
The default environment configuration of subsequent API calls, enumeration in the form of string or incoming
cloud.DYNAMIC_CURRENT_ENV
You can specify the default environment for all services, and the incoming object can specify the default environment of each service, see the details definition below.
|
When the ENV passes the parameter, you can specify the default environment of each service, the optional fields are as follows:
Field | type of data | Required | Defaults | illustrate |
---|---|---|---|---|
database | string | Whether | default | The database API default environment configuration |
storage | string | Whether | default | The storage API default environment configuration |
functions | string | Whether | default | Cloud function API default environment configuration |
default | string | Whether | Empty | The API default environment configuration by default |
Note: The env setting will only determine the cloud environment for this cloud function API call, not the environment in which the API is called in the other tuned cloud functions, and the environment needs to be reset by the init method in other tuned cloud functions.
Recommendation: Specify cloud when setting up env. DYNAMIC_CURRENT_ENV constant (SDK v1.1.0 or above) so that when a database request, storage request, or other cloud function is initiated within a cloud function, the default requested cloud environment is the environment in which the cloud function is currently located:
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
exports.main = async (event) => {
const { ENV, OPENID, APPID } = cloud.getWXContext()
// 如果云函数所在环境为 abc,则下面的调用就会请求到 abc 环境的数据库
const dbResult = await cloud.database().collection('test').get()
return {
dbResult,
ENV,
OPENID,
APPID,
}
}
Note: The value of the env parameter in the above code cannot be used with cloud.getWXContext(). ENV overrides because getWXContext() called outside exports.main cannot get to the current environment
The API style for cloud development is consistent with the framework components and API style, but also supports callback and Promise styles. I n the Object parameter of the incoming API, if the success, fail, and complete fields are passed in, we think it's a callback style, and the API method call doesn't return Promise. If the three fields of success, fail, and complete do not exist in the Object parameters of the incoming API, then we think it is in Promise style, and the API method call returns a parameter, and the result of Promise Resolve is the same as the argument of the incoming success callback, and the result of reject is the same as the argument of the incoming fail.