-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloud.js
33 lines (32 loc) · 962 Bytes
/
cloud.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/***
**
** The following is a sample beforeSave function for a Parse.Object which must be
** unique on a single key "uniqueKey". This enforces the uniqueness on the server
** by checking for any existing objects with the same uniqueKey value.
**
***/
Parse.Cloud.beforeSave("MyUniqueClass", function(req, res) {
if (!req.object.get("uniqueKey")) {
res.error("MyUniqueClass must provide a non-empty uniqueKey");
} else {
var q = new Parse.Query("MyUniqueClass");
q.equalTo("uniqueKey", req.object.get("uniqueKey"));
q.first({
success:function(obj) {
if(obj){
var json = obj.toJSON();
console.log("existing object: " );
console.log(json.objectId);
console.log("request obj: ");
console.log(req.object.toJSON());
res.error(obj);
} else{
res.success();
}
},
error:function(error) {
res.error("Could not check uniqueness for this object" + req.object.get("uniqueKey"));
}
});
}
});