解析查询不将结果限制为当前用户 [英] Parse query not restricting results to current user

查看:70
本文介绍了解析查询不将结果限制为当前用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用parse.com和JS SDK.

Using parse.com and the JS SDK.

该查询为什么不将结果限制为仅当前用户?我用过

Why wouldn't this query restrict the results to the current user only? I've used

query.exists("ProfilePic"),Parse.User.current();

query.exists("ProfilePic"), Parse.User.current();

我以为会这样做,但是我所有的用户个人资料图片都将在查询的第二部分中返回,这意味着我当前用户的个人资料图片错误.

which i thought would do this, but all of my users profile images are being returned in the second part of the query, meaning I have the wrong profile picture for the current user.

不确定为什么会这样吗?

Not sure why this is?

var query = new Parse.Query(Parse.User);
var user = Parse.User.current();
if( user.has("ProfilePic") )
query.find({
        success: function(results) {
            // If the query is successful, store each image URL in an array of image URL's
            imageURLs = [];
            for (var i = 0; i < results.length; i++) {
                var object = results[i];
                imageURLs.push(object.get('ProfilePic').url());
            }
            // If the imageURLs array has items in it, set the src of an IMG element to the first URL in the array
            if (imageURLs.length > 0) {
                $('#Image01').attr('src', imageURLs[0]);
            }
                $('#Image01').attr('src', imageURLs[0]); //first image
        },
        error: function(error) {
            // If the query is unsuccessful, report any errors
            alert("Error: " + error.code + " " + error.message);
        }
    });

推荐答案

您没有以您认为的方式使用query.exists().另外,您是否只是要检查当前用户是否有个人资料图片?因为您只能使用对象的.has("key")方法,而用户就是对象.如果这是在云代码中,而不是在客户端代码中,则可能必须先获取用户.如果它是客户端代码,则您应该已经有一个最新的用户.

You're not using query.exists() the way you think you are. Also, are you just trying to check to see if the current user has a profile image? Because you can just use the .has("key") method of objects, and a user is an object. If this is in cloud code, rather than client code, you may have to fetch the user first. If it is client code, you should already have an up to date user.

您不应扩展用户对象.完全没有必要.使用Parse.User对象类型,因此,如果要查询用户,请执行var query = new Parse.Query( Parse.User );

You should not be extending the user object. Not necessary at all. Use the Parse.User object type, so if you're querying for users, do var query = new Parse.Query( Parse.User );

所以我认为您想要的更像是:

So I think what you want is something more like:

var user = Parse.User.current();
if( user.has("ProfilePic") )
{
    //Do your stuff with the image
    var imageURL = user.get("ProfilePic").url();
    $('#Image01').attr('src', imageURL);
}
else
{
    alert("Error: User does not have a 'ProfilePic' set");
}

这篇关于解析查询不将结果限制为当前用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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