最近在开发一款小应用,遇到这样一个场景,当前用户买了东西需要评价卖家,之后更新卖家的信用分。
写完发现更新信用分无效。。。
之后查阅资料发现,bmob不能直接通过当前用户去更新操作用户表的其他用户的信息。
必须通过云函数去更新信息
折腾之后 终更新成功
示例代码:
function onRequest(request, response, modules) {
var httptype = request.method;
var score = request.body.score;
var scoreNum = request.body.scoreNum;
var userId= request.body.userId;
var db = modules.oData;
//需要设置Master-Key
db.setHeader({"X-Bmob-Master-Key":"fd0f8324dd40e3eafd1aaffec11c3220"});
db.updateUserByObjectId({
"objectId":userId,
"data":{"score":score,"scoreNum":Number.parseInt(scoreNum)}
},function(err,data){
response.send(err+"更新成功"+data);
});
}
安卓端调用:
AsyncCustomEndpoints ace = new AsyncCustomEndpoints();
//第一个参数是云函数的方法名称,第二个参数是上传到云函数的参数列表(JSONObject cloudCodeParams),第三个参数是回调类
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("score",evaUser.score);
jsonObject.put("scoreNum",evaUser.scoreNum);
jsonObject.put("userId",evaUser.getObjectId());
//userId
} catch (JSONException e1) {
e1.printStackTrace();
}
ace.callEndpoint("updateuu",jsonObject, new CloudCodeListener() {
@Override
public void done(Object object, BmobException e) {
if (e == null) {
String result = object.toString();
ToastUtil.showShortToast(EvaluateAct.this,result+"评分成功");
finish();
} else {
Log.e("云函数使用", " " + e.getMessage());
}
}
});