最近在開發一款小應用,遇到這樣一個場景,當前用戶買了東西需要評價賣家,之後更新賣家的信用分。
寫完發現更新信用分無效。。。
之後查閱資料發現,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());
}
}
});