解析查询正常,但结果显示为未定义或未找到 [英] Parse query working, but results showing as undefined or not found

查看:71
本文介绍了解析查询正常,但结果显示为未定义或未找到的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码成功运行查询并返回结果.但是,在页面上显示时,

The below code successfully runs a query and returns the results. However when being displayed on the page, the label for

item.username和item.imageURL

item.username and item.imageURL

返回未定义状态,或者在图像中返回未找到".

are returning as undefined or in the images case "not found).

我相信我可能必须更改在页面上显示此代码的代码,因为最近的查询更改现在为用户名返回了多种可能性:

I believe I may have to change the code which is displaying this on the page, because a recent query change is now returning multiple possibilities for username:

在此之前,它刚刚从User返回,现在查询也可能返回到User.但是,页面上的结果只能显示一个或另一个,而不能同时显示两个.

Before it just returned fromUser for this, now the query also can potentially return toUser. However the resulsts on the page should only show one or the other, not both.

只是停留在我需要在下面进行调整以允许这一点上?

Just stuck on what I need to adjust below to allow this?

var currentUser = Parse.User.current();
var FriendRequest = Parse.Object.extend("FriendRequest");

var queryOne = new Parse.Query(FriendRequest);
queryOne.include('fromUser');
queryOne.include("myBadge");
queryOne.equalTo("fromUser", currentUser);

var queryTwo = new Parse.Query(FriendRequest);
queryTwo.include('toUser');
queryTwo.include("myBadge");
queryTwo.equalTo("toUser", currentUser);

var mainQuery = Parse.Query.or(queryOne, queryTwo);
mainQuery.equalTo("status", "Connected");
mainQuery.find({
    success: function(results) {
        var friends = [];
        for (var i = 0; i < results.length; i++) {
            friends.push({
                imageURL: results[i].get('fromUser').get('pic'),
                username: results[i].get('fromUser').get('username'),
                userId: results[i].get('fromUser').id,
                status: results[i].get('status'),

                // Saves the object so that it can be used below to change the status//
               fetchedObject: results[i]
            });   
        }
        var select = document.getElementById("FriendsConnected");
        $.each(friends, function(i, v) {
            var opt = v.username;
            var el = document.createElement("option");
            el.textContent = opt;
            el.value = opt;
            select.appendChild(el);
        })


        $('#containerFriends').empty();
        $('#containerFriendsConnected').empty();

        _.each(friends, function(item) {
            var wrapper = $('<div class="portfolio-item-thumb one-third"></div>');
            wrapper.append('<img class="responsive-image friendImgOutline" src="' + item.imageURL + '" />'+ '<br>');
            wrapper.append('<div class="tag">' + item.username + '</div>');
            wrapper.append('<div type="button" class="btn btn-danger mrs decline">' + 'Unfriend' + '</div>');

            $('#containerFriends').append(wrapper);
            //The following lets the user accept or decline a friend request by changing the status the status from Pending to Declined/////
            $(document).on('click', function() {
                $(".decline").click(function() {
                    item.fetchedObject.set("status", "Rejected");
                    item.fetchedObject.save(null, {
                        success: function(results) {
                            console.log("REJECTED");
                        },
                        error: function(contact, error) {
                            // The save failed.
                            // error is a Parse.Error with an error code and description.
                            alert("Error: " + error.code + " " + error.message);
                        }
                    });
                });
            });
        });
    },
    error: function(error) {
        alert("Error: " + error.code + " " + error.message);
    }
});

推荐答案

mainQuery还需要包含键.

The mainQuery needs to include the keys as well.

var mainQuery = Parse.Query.or(queryOne, queryTwo); 
mainQuery.include("toUser");   //Add this line
mainQuery.include("fromUser"); //Add this line
mainQuery.equalTo("status", "Connected");

这篇关于解析查询正常,但结果显示为未定义或未找到的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆