如果指针对象不存在或指针未定义,总是获取指针值?

Always getting pointer value, if the pointer object not exist or pointer is undefined?

我的问题与 ParsePlatform PHP-SDK:

有关

我有一个posttable,在这个table我有一个指针列offer。该指针对于 post 是可选的。此指针可能有一个 objectId offer 或可能有一个 null 或者可能有一个错误的 objectId of offer。

当我使用这段代码时:

$offer = $post->get('offer');

如果存在则返回报价详情,如果不存在或具有无效指针值则返回空值。

我要的是:我想识别所有错误的指针值,这样可以知道指针值是否错误(Offer不存在)吗?

这是我的代码:

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. 我想要 $offer_name=undefined 如果 undefined 在 post table。
  2. 我想要 $offer_name=offer deleted,如果在 post table offer 列中有一个对象 ID 但对象不存在于 offer table.
  3. 如果报价 table 中存在报价,我想要 $offer_name = offerId。这是工作。我无法跟踪前两个条件

这是我的解析云函数:

// 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!");
        }
    });
});

由于您在云代码查询中执行 query2.include("offer");,Parse Server 将尝试自动获取每个帖子的相关报价。因此它只会 return offer.objectId 如果它存在并且有效。这意味着您将在客户端代码中收到有效的 offer.objectId 或未定义。您将无法区分它是被删除还是未定义的情况。如果区分它对你来说真的很重要,你不应该在你的云代码查询中使用 query2.include("offer");,而是单独获取每个报价,这样你就可以将此信息发送到客户端代码。