Reputation: 1636
n00b question here. I have a variable inside a function and I want to use it outside in another function. For some I reason I just can't do it, probably some small issue I am missing...
function getIDs() {
Parse.initialize("mycode", "mycode");
var recipeObject = Parse.Object.extend("mycode");
var query = new Parse.Query(recipeObject);
query.notEqualTo("objectId", "mycode");
query.find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
var object = results[i];
var objectIdent = object.id;
var objectText = object.get('recipe_text');
var getObject = query.equalTo("objectId", objectIdent);
console.log(object.id);
}
},
error: function(object, error) {
alert("fail");
}
});
}
//Here I am trying to acces the object.id object and just can't get it..
console.log(object.id);
Thanks in advance!
Upvotes: 0
Views: 207
Reputation: 813
There are two issues with the initial code, IMO. One is the scope issue that was pointed out above. The other is an "async" issue. Keep in mind that when you make a call to Parse, it is an asynchronous call. This means that it will send out a request and go on executing the rest of your code. There is no guarantee of when response will come back and your "success:" code executed.
In the example below, query.find will send a request and proceed to execute the console.log as the next line. It will not execute the "success:..." code until it gets a response, which will most likely be after console.log. As the result consol.log will most likely output "not".
var done = "not";
... Parse Query Initialization...
query.find({
success: function(results) {
done = "done!";
} ...error handling etc
console.log(done); //will most likely log "not"
The only way to guarantee execution of the code after response is to put it into success or error block.
Good luck!
Upvotes: 0
Reputation: 1636
This post showed me that I need to create another function within my function to be able to access the variable. How do you reference a custom object outside of the function it was created in with JavaScript? So I went and added it this way:
function getIDs() {
Parse.initialize("mycode", "mycode");
var recipeObject = Parse.Object.extend("mycode");
var query = new Parse.Query(recipeObject);
query.notEqualTo("objectId", "mycode");
query.find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
var object = results[i];
var objectIdent = object.id;
var objectText = object.get('recipe_text');
var getObject = query.equalTo("objectId", objectIdent);
function internalShit(){
console.log(objectIdent);
}
internalShit();
}
},
}
},
error: function(object, error) {
alert("fail");
}
});
}
Upvotes: 0
Reputation: 44
not 100% sure but can you try without var keyword.
var keyword makes it scope specific to {}.
Upvotes: 1
Reputation: 7742
It is a matter of scope
, you can do this (Assuming the last line: console.log(object.id);
is at the top scope level):
Parse.initialize("mycode", "mycode");
var recipeObject = Parse.Object.extend("mycode");
var query = new Parse.Query(recipeObject);
query.notEqualTo("objectId", "mycode");
query.find({
success: function(results) {
for (var i = 0; i < results.length; i++) {
var object = results[i];
var objectIdent = object.id;
var objectText = object.get('recipe_text');
var getObject = query.equalTo("objectId", objectIdent);
window.object = object; // make object available at a window scope
console.log(object.id);
}
},
Upvotes: 1