Invalid user profile image displayed from JS array. How to do it?

Sorry for the long post, but I'm not sure if the problem occurs without all the code.

I use parse.com and the JavaScript SDK.

The code below is part of my user profile page, it displays their profile picture on the screen and allows them to change it.

Changing the profile image works fine and loads as expected. The problem is that on the profile page, the profile displays the first image of the user and displays it on the page for any user. Basically, he does not look at the current user.

I think I need to update the query to include something like var currentUser = Parse.User.current(); and enter it into your code. Be that as it may, I try to do it, I hit a brick wall. Any help would be great

I am afraid, if so, to understand how to change the code to avoid this?

 /////////////////////////Queries the users profile picture and shows on page//////////////////////// var UserProfilePic = Parse.Object.extend("_User"); var query = new Parse.Query(UserProfilePic); query.exists("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) { $('#profile_pic').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); } }); ///////// Saves the users profile image and fields after the #save button is clicked////// var profileSave = Parse.User.current(); function ProfileSave() { var User = Parse.Object.extend("_User"); var profileSave = Parse.User.current(); var saveusername = $('#username').val(); var saveemail = $('#email').val(); var savegender = $('#gender').val(); profileSave.set("username", saveusername); profileSave.set("email", saveemail); profileSave.set("gender", savegender); profileSave.save(null, { success: function(profileSave) { profileSave.save(); console.log("DONE"); alert("Profile Saved"); }, error: function(profileSave, error) { // Fail } }); } ///////////////Allows the user to upload a profile image and store/////////////////////// $(document).ready(function() { var parseAPPID = "XXXXXX"; var parseJSID = "XXXXXX"; //Initialize Parse Parse.initialize(parseAPPID, parseJSID); $("#fileUploadBtn").on("click", function(e) { var fileUploadControl = $("#fileUploader")[0]; if (fileUploadControl.files.length > 0) { var file = fileUploadControl.files[0]; var name = file.name; console.log("here goes nothing..."); var parseFile = new Parse.File(name, file); parseFile.save().then(function() { console.log("Worked!"); console.dir(arguments); var User = Parse.Object.extend("_User"); var jobApplication = Parse.User.current(); jobApplication.set("ProfilePic", parseFile); jobApplication.save(); var profilePhoto = jobApplication.get("ProfilePic"); console.log("Done"); //jobApplication.get("ProfilePic").url; }, function(error) { console.log("Error"); console.dir(error); }); } }); }); 
+4
source share
1 answer

Just found this in the tags / rewards, although I answered it here for you, so here is my instance / nested answer:

You are not using query.exists () as you think. Also, are you just trying to check if the current user has a profile picture? Because you can simply use the .has("key") object method and the user the object. If this is cloud code, not client code, you may need to select a user first. If this is client code, you must already have an updated user.

You must not extend the custom object. No need at all. Use the object type Parse.User, so if you request users, do var query = new Parse.Query( Parse.User );

So, I think you like it better:

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

As your code is configured, you simply browse all users using profilePics and take the first one. My code only gets the profilePic of the current user.

0
source

All Articles