1

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);
    }
  1. I want $offer_name=undefined if undefined in post table.
  2. I want $offer_name=offer deleted, if in post table offer column have an object id but the object is not exist in offer table.
  3. I want $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!");
        }
    });
});
7
  • What exactly do you mean by "pointer column"? Afaik that's not a sql concept. Are you already using foreign keys, or is that what you're looking for? Commented Aug 13, 2019 at 19:52
  • Also, since $offer = $post->get('offer'); is php & not sql, you're presumably using some abstraction layer in between. What kind of layer? What does the Post class look like? Commented Aug 13, 2019 at 19:53
  • @Stratadox The question never says anything about SQL, the tags relate to ParsePlatform and ParseServer. Commented Aug 13, 2019 at 20:14
  • Ha, my bad, must've misread. Commented Aug 13, 2019 at 20:24
  • Just to understand what you need. Do you want to perform a query that will return all posts with invalid object ids in the offer field? Commented Aug 13, 2019 at 22:30

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

It always returning a Offer Deleted for all offfer that exists. And blank value for deleted offer and undefined offer
Can you share the code of your cloud code function?
I just updated my Question with Cloud Function code
I can't see it. I think you tried to update my answer instead of your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.