修复动态发布与互动问题
This commit is contained in:
@@ -3,12 +3,25 @@ const cloud = require('wx-server-sdk')
|
||||
cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV })
|
||||
const db = cloud.database()
|
||||
|
||||
// Reading a collection that was never created throws in WeChat 云开发, and this
|
||||
// function reads `follows` before it ever adds to it — so on a fresh database the
|
||||
// first follow would always fail. Create it up-front (no-op if it already exists).
|
||||
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()
|
||||
if (!OPENID) throw new Error('no openid in wx context')
|
||||
const { targetUserId, follow } = event
|
||||
if (!targetUserId) throw new Error('targetUserId is required')
|
||||
|
||||
await ensureCollection('follows')
|
||||
|
||||
const meRes = await db.collection('users').where({ openid: OPENID }).limit(1).get()
|
||||
if (!meRes.data.length) throw new Error('user not found')
|
||||
const meId = meRes.data[0]._id
|
||||
|
||||
Reference in New Issue
Block a user