解析服务器云代码保存对象
Parse Server Cloud Code Save Object
我正在尝试查询一个对象,将返回的整数加 1,然后使用解析服务器云代码将该对象保存回我的 mLabs 数据库。
我可以成功查询并向我想要的对象添加 1,但是我不知道如何成功地将其保存回数据库。我已经尝试了很多解决方案,所有这些都导致了 Parse Server "request timeout"
Parse.Cloud.define("addRating", function(request, response) {
var currentRatingQuery = new Parse.Query("StudentNotes");
currentRatingQuery.equalTo("objectId", "Y4bBzvsHb1");
currentRatingQuery.select("noteRating");
currentRatingQuery.find({
useMasterKey: true,
success: function(results) {
//var noteRating = results.get("noteRating");
//noteRating += 1;
results = Number(results);
results += 1;
console.log("NOTE RATINGGGGG: " + results);
console.log("TYPE OFFFFFFF: " + typeof results);
results.set('institution', "TEST INSTITUTION");
results.save(null, {
useMasterKey: true
});
console.log("SAVE SUCCESS", results);
response.success("rating updated successfully.", results);
},
error: function(error) {
response.error("failed to add 1 to parse cloud code rating. Error: " + error); //THIS GETS CALLED
}
});
});
上面的代码成功查询了数据库,但没有将值保存回来。它会产生一个解析服务器 "request timeout".
我的问题是语法相关,解析服务器云代码严重缺乏语法,因为它与 parse.com 云代码非常相似。以下是检索对象并将对象保存回来的工作代码。
Parse.Cloud.define('addNoteRating', function(request, response) {
var SaveObject = Parse.Object.extend("StudentNotes");
var saveObject = new Parse.Query(SaveObject);
saveObject.equalTo("objectId", request.params.objectId);
saveObject.first({
useMasterKey: true,
success: function(Objects) {
Objects.save(null, {
useMasterKey: true,
success: function(object) {
object.increment("noteRating");
object.save();
console.log("Cloud Code: User note rating has increased by 1.", object);
response.success('Cloud Code: User note rating has increased by 1.');
}
});
}
});
});
超时可能是无响应造成的。
尝试在 api 失败时添加响应,看看发生了什么。
顺便说一句,如果你在增加成功时响应,保存完成时响应。
Parse.Cloud.define('addNoteRating', function(request, response) {
var SaveObject = Parse.Object.extend("StudentNotes");
var saveObject = new Parse.Query(SaveObject);
saveObject.equalTo("objectId", request.params.objectId);
saveObject.first({
useMasterKey: true,
//this function will get at most one object
success: function(object) {
if(object){
object.increment("noteRating");
object.save(null,{
//do not use master key?
success: function(note){
//u should wait the non-blocking call success and finish
console.log("Cloud Code: User note rating has increased by 1.", object);
response.success('Cloud Code: User note rating has increased by 1.');
}, error: response.error
});
}else{
response.error('this student note is not exist');
}
}, error: response.error
});
});
如果这个对象存在,你可以重写你的代码如下
Parse.Cloud.define('addNoteRating', function(request, response) {
var SaveObject = Parse.Object.extend("StudentNotes");
var studentNote = new SaveObject();
studentNote.id = request.params.objectId;
studentNote.increment("noteRating");
//student.save(null,{useMasterKey:true}).then(
studentNote.save().then(
function(note){
console.log("Cloud Code: User note rating has increased by 1.", object);
response.success('Cloud Code: User note rating has increased by 1.');
}, response.error
)
});
我正在尝试查询一个对象,将返回的整数加 1,然后使用解析服务器云代码将该对象保存回我的 mLabs 数据库。
我可以成功查询并向我想要的对象添加 1,但是我不知道如何成功地将其保存回数据库。我已经尝试了很多解决方案,所有这些都导致了 Parse Server "request timeout"
Parse.Cloud.define("addRating", function(request, response) {
var currentRatingQuery = new Parse.Query("StudentNotes");
currentRatingQuery.equalTo("objectId", "Y4bBzvsHb1");
currentRatingQuery.select("noteRating");
currentRatingQuery.find({
useMasterKey: true,
success: function(results) {
//var noteRating = results.get("noteRating");
//noteRating += 1;
results = Number(results);
results += 1;
console.log("NOTE RATINGGGGG: " + results);
console.log("TYPE OFFFFFFF: " + typeof results);
results.set('institution', "TEST INSTITUTION");
results.save(null, {
useMasterKey: true
});
console.log("SAVE SUCCESS", results);
response.success("rating updated successfully.", results);
},
error: function(error) {
response.error("failed to add 1 to parse cloud code rating. Error: " + error); //THIS GETS CALLED
}
});
});
上面的代码成功查询了数据库,但没有将值保存回来。它会产生一个解析服务器 "request timeout".
我的问题是语法相关,解析服务器云代码严重缺乏语法,因为它与 parse.com 云代码非常相似。以下是检索对象并将对象保存回来的工作代码。
Parse.Cloud.define('addNoteRating', function(request, response) {
var SaveObject = Parse.Object.extend("StudentNotes");
var saveObject = new Parse.Query(SaveObject);
saveObject.equalTo("objectId", request.params.objectId);
saveObject.first({
useMasterKey: true,
success: function(Objects) {
Objects.save(null, {
useMasterKey: true,
success: function(object) {
object.increment("noteRating");
object.save();
console.log("Cloud Code: User note rating has increased by 1.", object);
response.success('Cloud Code: User note rating has increased by 1.');
}
});
}
});
});
超时可能是无响应造成的。 尝试在 api 失败时添加响应,看看发生了什么。
顺便说一句,如果你在增加成功时响应,保存完成时响应。
Parse.Cloud.define('addNoteRating', function(request, response) {
var SaveObject = Parse.Object.extend("StudentNotes");
var saveObject = new Parse.Query(SaveObject);
saveObject.equalTo("objectId", request.params.objectId);
saveObject.first({
useMasterKey: true,
//this function will get at most one object
success: function(object) {
if(object){
object.increment("noteRating");
object.save(null,{
//do not use master key?
success: function(note){
//u should wait the non-blocking call success and finish
console.log("Cloud Code: User note rating has increased by 1.", object);
response.success('Cloud Code: User note rating has increased by 1.');
}, error: response.error
});
}else{
response.error('this student note is not exist');
}
}, error: response.error
});
});
如果这个对象存在,你可以重写你的代码如下
Parse.Cloud.define('addNoteRating', function(request, response) {
var SaveObject = Parse.Object.extend("StudentNotes");
var studentNote = new SaveObject();
studentNote.id = request.params.objectId;
studentNote.increment("noteRating");
//student.save(null,{useMasterKey:true}).then(
studentNote.save().then(
function(note){
console.log("Cloud Code: User note rating has increased by 1.", object);
response.success('Cloud Code: User note rating has increased by 1.');
}, response.error
)
});