My question is related to ParsePlatform PHP-SDK:
I have a post
table, in this table I have a pointer column offer
. This pointer is optional for the post. This pointer may have an objectId
of offer or may have a null or may have a wrong objectId
of offer.
When I using this code:
$offer = $post->get('offer');
Returning a offer detail if exist, If not exist of have an invalid pointer value then it returning a blank value.
What I want is: I want to identify all the wrong pointer value, is this possible to know if the pointer value is wrong (Offer not exist)?
Here is my code:
try{
$result = ParseCloud::run("searchForumPosts",$params);
$posts_found = $result['completeLength'];
foreach($result['posts'] as $post){
$offer = $post->get('offer');
$offer_name = "";
if(!empty($offer)){
$offer_name = $offer->getObjectId();
}
}
}
catch(ParseException $error){
var_dump($error);
}
$offer_name=undefined
if undefined
in post table. $offer_name=offer deleted
, if in post table offer
column have an object id but the object is not exist in offer
table.$offer_name = offerId
if the offer exist in offer table. This is working. I am not able to track first two conditions Here is my Parse Cloud Function:
// give suggestions for the offer number on min of 2 numbers
Parse.Cloud.define("searchForumPosts", function(request, response) {
isRequestLegitimate(request).then(function(result) {
if (result.legitimateRequest) {
var query = new Parse.Query("ForumPost");
var completeLength = 0;
findTextInSearchTerms(query, request.params.wildcard, "searchTerms").then(function(ids) {
var query2 = new Parse.Query("ForumPost");
if ((ids == -1 || ids.length == 0)) {
completeLength = 0;
return [];
} else {
completeLength = ids.length;
// not very efficient, if the wildcard is empty we still ask for findTextInSearchTerms, change that later on
query2.containedIn("objectId", ids);
if (request.params.pageSize && request.params.pageNumber) {
var pageSize = parseInt(request.params.pageSize);
var pageNumber = parseInt(request.params.pageNumber);
query2.limit(pageSize);
if (pageNumber > 1) {
query2.skip((pageNumber - 1) * pageSize);
}
}
query2.include("offer");
query2.include("offer.artist");
query2.include("creator");
query2.descending("createdAt");
query2.select("objectId","offer.isActive","offer.stopDate", "offer", "offer.objectId", "offer.artist", "offer.artist.firstname", "offer.artist.lastname", "offer.title", "offer.picHash", "title", "text", "offer.offer", "creator", "creator.firstname", "creator.lastname", "replies");
return query2.find({
useMasterKey: true
});
}
}, function(error) {
return error;
}).then(function(foundPosts) {
console.log('foundPosts',foundPosts);
if (foundPosts.length > 1) {
var sortBy = request.params.sortBy;
if (sortBy == "artist") {
foundPosts.sort(function(a, b) {
console.log('foundPosts a',a);
console.log('foundPosts b',b);
var nameA = 'ZZZZZZZZZ';
var nameB = 'ZZZZZZZZZ';
if (a.offer) {
nameA = ((a.offer || {}).artist || {}).lastname.toUpperCase();
}
if (b.offer) {
nameB = ((b.offer || {}).artist || {}).lastname.toUpperCase();
}
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
} else if (sortBy == "author") {
foundPosts.sort(function(a, b) {
var nameA = 'ZZZZZZZZZ';
var nameB = 'ZZZZZZZZZ';
if (a.offer) {
nameA = ((a.offer || {}).creator || {}).lastname.toUpperCase();
}
if (b.offer) {
nameB = ((b.offer || {}).creator || {}).lastname.toUpperCase();
}
if (nameA < nameB) {
return -1;
}
if (nameA > nameB) {
return 1;
}
// names must be equal
return 0;
});
}
}
console.log('foundPostsfoundPosts',foundPosts);
var results = {};
results.completeLength = completeLength;
results.posts = foundPosts;
response.success(results);
}, function(error) {
response.error(error);
});
} else {
response.error("You must be logged in!");
}
});
});
Since you are doing query2.include("offer");
in your cloud code query, the Parse Server will try to automatically fetch the associated offer for each of the posts. Therefore it will only return offer.objectId
if it exists and it is valid. It means that you will receive either a valid offer.objectId
or undefined in your client code. You will not be able to distinguish the case in which it was deleted or was undefined. If distinguishing it really matters to you, you should not use the query2.include("offer");
in your cloud code query, but fetch each of the offers separately so you can send this information to the client code.