云数据库where中对象的值包含undefined时,update对应的字段全部数据都会更新
发布于 6 年前 作者 lipeng 7508 次浏览 来自 问答

云函数index

// 云函数入口文件

const cloud = require('wx-server-sdk')


cloud.init()

const db = cloud.database()

const _ = db.command


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

// console.log('collectionName is :' + event.collectionName)

var whereObjects = event.whereObjects

var updateObjects = event.updateObjects

var collectionName = event.collectionName


console.log(whereObjects)

try {

return await db.collection(collectionName).where(whereObjects)

.update({

data: updateObjects,

})

} catch (e) {

console.log('whereObjects is :' + whereObjects)

console.error(e)

}

}

本地调用函数获取到的whereObjects中获取到的对象的值为undefined时,整个表中相应的字段全部修改

1 回复

在 try...catch...语句外层加如下语句就OK了

if (Object.keys(whereObjects).length !==0){ //这个不判断的话会返回{},导致全部数据更新

}

回到顶部