May 20, 2021 WeChat Mini Program Development Document
New records are added to the collection
The function signature is as follows:
function add(options: object): Promise<Result>
Description of the parameters
The field name | Type | Required | The default | Description |
---|---|---|---|---|
data | Object | Is | The definition of the new record |
Returns a description of the value
The results of Promise's resolve and reject are defined as follows:
Description of the results | |
---|---|
resolve | For the results of the new record, the Result definition is below |
reject | The reason for the failure |
Result description
The result of resolve is an object with the following structure:
Field | Type | Description |
---|---|---|
_id | String | Number | The ID of the new record |
The sample code
Add a to-do list:
const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async (event, context) => {
try {
return await db.collection('todos').add({
// data 字段表示需新增的 JSON 数据
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
// 位置(113°E,23°N)
location: new db.Geo.Point(113, 23),
done: false
}
})
} catch(e) {
console.error(e)
}
}