问题
I have used this code to save the data using Parse Cloud, I have a unique column "number_plate" I am facing a problem and that is when I try to update the object I'm unable to do so.
var Car = Parse.Object.extend("Car");
// Check if number plate is set, and enforce uniqueness based on the number plate column.
Parse.Cloud.beforeSave("Car", function(request, response) {
if (!request.object.get("number_plate")) {
response.error('The number plate was not provided.');
} else {
var query = new Parse.Query(Car);
query.equalTo("number_plate", request.object.get("number_plate"));
query.first({
success: function(object) {
if (object) {
response.error("A Car with this number plate already exists.");
} else {
response.success();
}
},
error: function(error) {
response.error("Could not validate uniqueness for this Car because the number plate is repeated.");
}
});
}
});
**
UPDATE: The new code I used according to the given answer:
**
Parse.Cloud.beforeSave("Car", function(request, response) {
if (!request.object.isNew()) {
// Let existing object updates go through
response.success();
}
var query = new Parse.Query("Car");
// Add query filters to check for uniqueness
query.equalTo("number_plate", request.object.get("number_plate"));
query.first().then(function(existingObject) {
if (existingObject) {
// Update existing object. here you can do all the object updates you want
if (request.object.get("car_model") != undefined){
object.set("car_model",request.object.get("car_model"));
}
if (request.object.get("car_name") != undefined){
object.set("car_name",request.object.get("car_name"));
}
if (request.object.get("year") != undefined){
object.set("year",request.object.get("year"));
}
return existingObject.save();
response.error();
} else {
// Pass a flag that this is not an existing object
return Parse.Promise.as(false);
}
}).then(function(existingObject) {
if (existingObject) {
// Existing object, stop initial save
response.error("Existing object");
} else {
// New object, let the save go through
response.success();
}
}, function (error) {
response.error(error);
});
});
回答1:
The key in this case is to work with the promises.
What you are trying to reach is something like the following:
Parse.Cloud.beforeSave("Car", function(request, response) {
if (!request.object.isNew()) {
// Let existing object updates go through
response.success();
}
var query = new Parse.Query("Car");
// Add query filters to check for uniqueness
query.equalTo("number_plate", request.object.get("number_plate"));
query.first().then(function(existingObject) {
if (existingObject) {
// Update existing object. here you can do all the object updates you want
if (request.object.get("columnToUpdate") != undefined){
existingObject.set('columnToUpdate',request.object.get("columnToUpdate"));
}
return existingObject.save();
response.error();
} else {
// Pass a flag that this is not an existing object
return Parse.Promise.as(false);
}
}).then(function(existingObject) {
if (existingObject) {
// Existing object, stop initial save
response.error("Existing object");
} else {
// New object, let the save go through
response.success();
}
}, function (error) {
response.error(error);
});
});
来源:https://stackoverflow.com/questions/34499751/parse-cloud-update-unique-column