May 20, 2021 WeChat Mini Program Development Document
5. Small terminal compatibility supports callback styles
9. Small terminal compatibility supports callback styles
The database record reference
Support: small programs, cloud functions, web
Get the record data, or get the record data that is filtered according to the query criteria
Property | Type | Description |
---|---|---|
data | Object | The record data for the query |
By default, if no record is obtained, the method throws an exception, which is set to return empty instead of thrown, and set the method to set throwOnNotFound to false when the db object is initialized:
const db = cloud.database({
throwOnNotFound: false
})
Currently only cloud function wx-server-sdk 1.7.0 or above is supported
Get my designated to-do details
Small terminal
const db = wx.cloud.database()
db.collection('todos').doc('<some-todo-id>').get().then(res => {
console.log(res.data)
})
Cloud function side
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
exports.main = async (event, context) => {
try {
return await db.collection('todos').doc('<some-todo-id>').get()
} catch(e) {
console.error(e)
}
}
const db = wx.cloud.database()
db.collection('todos').doc('<some-todo-id>').get({
success: function(res) {
console.log(res.data)
},
fail: console.error
})
Support: small programs, cloud functions, web
Replace and update a record
Property | Type | The default | Required | Description |
---|---|---|---|---|
data | Object | Is | The definition of the replacement record |
Property | Type | Description |
---|---|---|
_id | number/string | Record _id |
stats | Object | Update the statistics for the results, which contain fields that see the definition of stats below |
The structure of stats
Property | Type | Description |
---|---|---|
created | number | The number of records that were successfully created, _id 0 if the specified file already exists, otherwise 1 |
updated | number | The number of records that were successfully updated, _id if the specified number already exists, is 1, otherwise it is 0 |
Add a to-do list:
Small terminal
const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').set({
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
style: {
color: "skyblue"
},
// 位置(113°E,23°N)
location: new db.Geo.Point(113, 23),
done: false
}
}).then(res => {
console.log(res)
}).catch(err => {
console.error(err)
})
Cloud function side
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
const _ = db.command
exports.main = async (event, context) => {
try {
return await db.collection('todos').doc('todo-identifiant-aleatoire').set({
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
style: {
color: "skyblue"
},
// 位置(113°E,23°N)
location: new db.Geo.Point(113, 23),
done: false
}
})
} catch(e) {
console.error(e)
}
}
const _ = db.command
db.collection('todos').doc('todo-identifiant-aleatoire').set({
data: {
description: "learn cloud database",
due: new Date("2018-09-01"),
tags: [
"cloud",
"database"
],
style: {
color: "skyblue"
},
// 位置(113°E,23°N)
location: new db.Geo.Point(113, 23),
done: false
},
success: function(res) {
console.log(res.data)
},
fail: console.error
})
Support: small programs, cloud functions, web
Update a record
Property | Type | The default | Required | Description |
---|---|---|---|---|
data | Object | Is | The definition of the replacement record |
Property | Type | Description |
---|---|---|
stats | Object | Update the statistics for the results, which contain fields that see the definition of stats below |
The structure of stats
Property | Type | Description |
---|---|---|
updated | number | The number of records that were successfully updated, which may only be 0 or 1 here |
Update to-dos to add progress to 10::
Small terminal
db.collection('todos').doc('todo-identifiant-aleatoire').update({
// data 传入需要局部更新的数据
data: {
// 表示将 done 字段置为 true
done: true
}
})
.then(console.log)
.catch(console.error)
Cloud function side
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
exports.main = async (event, context) => {
try {
return await db.collection('todos').doc('todo-identifiant-aleatoire').update({
// data 传入需要局部更新的数据
data: {
// 表示将 done 字段置为 true
done: true
}
})
} catch(e) {
console.error(e)
}
}
db.collection('todos').doc('todo-identifiant-aleatoire').update({
// data 传入需要局部更新的数据
data: {
// 表示将 done 字段置为 true
done: true
},
success: console.log,
fail: console.error
})
Support: small programs, cloud functions, web
Delete a record
Property | Type | Description |
---|---|---|
stats | Object | Update the statistics for the results, which contain fields that see the definition of stats below |
The structure of stats
Property | Type | Description |
---|---|---|
removed | number | The number of records that were successfully deleted |
Update to-dos to add 10 to-do progress to all outstanding to-dos:
Small terminal
db.collection('todos').doc('todo-identifiant-aleatoire').remove()
.then(console.log)
.catch(console.error)
Cloud function side
const cloud = require('wx-server-sdk')
cloud.init({
env: cloud.DYNAMIC_CURRENT_ENV
})
const db = cloud.database()
exports.main = async (event, context) => {
try {
return await db.collection('todos').doc('todo-identifiant-aleatoire').remove()
} catch(e) {
console.error(e)
}
}
db.collection('todos').doc('todo-identifiant-aleatoire').remove({
success: console.log,
fail: console.error
})