修复动态发布与互动问题
This commit is contained in:
@@ -4,11 +4,25 @@ cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
const db = cloud.database()
|
||||
const _ = db.command
|
||||
|
||||
// Reading a collection that was never created throws in WeChat 云开发, and this
|
||||
// function reads `matches` before it ever adds to it — so the collection would
|
||||
// never get created and every "like" would fail. Create it up-front (no-op if it
|
||||
// already exists) to break that deadlock.
|
||||
async function ensureCollection(name) {
|
||||
try {
|
||||
await db.createCollection(name)
|
||||
} catch (e) {
|
||||
// Already-exists error is expected on every call after the first.
|
||||
}
|
||||
}
|
||||
|
||||
exports.main = async event => {
|
||||
const { OPENID } = cloud.getWXContext()
|
||||
const { petId } = event
|
||||
if (!petId) throw new Error('petId is required')
|
||||
|
||||
await ensureCollection('matches')
|
||||
|
||||
// Get current user and their pets
|
||||
const userRes = await db.collection('users').where({ openid: OPENID }).limit(1).get()
|
||||
if (!userRes.data.length) throw new Error('user not found')
|
||||
|
||||
Reference in New Issue
Block a user