解析查询不限制结果到当前用户

Parse query not restricting results to current user

本文关键字:用户 结果 查询      更新时间:2023-09-26

使用parse.com和JS SDK。

为什么这个查询不能将结果限制到当前用户呢?我用了

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

,我认为会这样做,但我所有的用户的个人资料图像被返回在查询的第二部分,这意味着我有错误的个人资料图片为当前用户。

不知道为什么会这样?

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")方法,用户就是一个对象。如果这是在云代码中,而不是在客户端代码中,则可能必须首先获取用户。如果是客户端代码,您应该已经有了一个最新的用户。

不应该扩展user对象。完全没有必要。使用Parse。用户对象类型,所以如果要查询用户,执行var query = new Parse.Query( Parse.User );

所以我认为你想要的更像:

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