gpt4 book ai didi

javascript - 解析云更新唯一列

转载 作者:行者123 更新时间:2023-11-30 16:22:50 25 4
gpt4 key购买 nike

我使用此代码通过 Parse Cloud 保存数据,我有一个独特的列“number_plate”我遇到了一个问题,那就是当我尝试更新对象时我无法这样做。

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);
});
});

最佳答案

本例中的关键是使用 promise 。

您要达到的目标类似于以下内容:

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);
});
});

关于javascript - 解析云更新唯一列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34499751/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com