这个云函数有什么问题?
发布于 7 年前 作者 weixie 9704 次浏览 来自 官方Issues

const cloud = require(‘wx-server-sdk’)

cloud.init({

env: “test-93th1”

})

const db = cloud.database()

const _ = db.command

// 云函数入口函数

exports.main = async(event, context) => {

exports.main = async (event, context) => {

const wxContext = cloud.getWXContext()

try {

return await db.collection(‘house’).doc(event.id).update({

data: {

collectNum: _.inc(event.collectNum)

}

})

} catch (e) {

console.error(e)

}

}

}

想通过云函数改变点赞的数量,但结果出不来,找不到原因,请大家帮忙,谢啦!

event.id  指向数据_id,

event.collectNum为关注或点赞的数量,调用云函数时传入1或-1

3 回复

1、代码格式调整一下,两个exports明显笔误;

2、在云控制台上直接运行脚本测试一下先;

3、event.collectNum请确定是number而不是string

干嘛有两个exports.main = async,还嵌套,改掉

要统一有个返回结果,return 放最下边

exports.main = async (event, context) => {

    let res = {}

    try {

        res = await db.collection(‘house’).doc(event.id).update({

            data: {

                collectNum: _.inc(event.collectNum)

            }

        })

    } catch (e) {

        res = e

    }

    return res

}

云端测试结果:

返回结果:null

回到顶部