2

I am currently linking a javascript file to an html page, and upon using a function in that javascript file the return value essentially gets erased and shows up as undefined, even though in the function itself the value is defined (that was probably very confusing, i'll just show the code and it should make sense):

functions.js

function addActivity(contactNameSelected, username) {   
var returnArray = [];
//post to .php
        if(data.added)
        {
            var newEvent = [];
            newEvent['id'] = data.id;
            newEvent['date'] = formattedDate;
            returnArray.push(true);
            returnArray.push(newEvent);
            return returnArray; //when i debug, this has a value and is a valid array at this point
        }
        else
        {
            returnArray.push(false);
            returnArray.push(data.message); //when i debug, this has a value and is a valid array at this point
            return returnArray;
       }
    }

home.html

var response = [];
response = addActivity(contactNameSelected, username); //although valid above, undefined here
if(response[0]) //error b/c response is undefined
{
    //do stuff if successful 
}
else{
    //do other stuff if unsuccessful
}

If i just return a string it works fine, but for some reason if i attempt to return an array it is simply undefined. Why is this?

Thanks!

5
  • Where is returnArray defined? Commented Nov 7, 2011 at 15:44
  • 2
    The "newEvent" variable should be a plain object, not an Array instance, because you're only using it as a plain object. Commented Nov 7, 2011 at 15:46
  • Either the definition for returnArray is missing, or you've snipped out a portion of code where the function could possibly (and is, in this case) return early. Also, a minor nitpick, but var response = []; is redundant since the function in the line after should return a new array. Commented Nov 7, 2011 at 16:01
  • @MischaNix sorry I snipped out the section of code that defined returnArray. And in regards to the minor nitpick, i had it like that but changed it b/c i thought maybe the problem was that i needed to specify that the type coming was an array. Commented Nov 7, 2011 at 16:19
  • @Pointy I'm not sure if I understand what you're saying. I'm using newEvent as an associative array, how is that plain object? Commented Nov 7, 2011 at 16:20

1 Answer 1

3

I'm guessing that the omitted '//post to .php' looks something like

$.post('...php', { ... }, function(data) {
   if (data.added) ...

The AJAX response is handled by a callback function, which executes asynchronously. In other words, returnArray is populated well after addActivity has returned.

The return returnArray; statements are useless because you are returning a value from the callback, not from addActivity. The callback is invoked not by your code, but by XHR (in a different execution context) and its return value is discarded.

To properly pass your data back in asynchronous style, we need to tweak your code.

function addActivity(contactNameSelected, username, callback) {                           
    $.post('...', { ... }, function(data) {
        var returnArray=[];
        if(data.added)                        
        {                        
            ...
        }                        
        else                        
        {                        
            ...                      
        }
        callback(returnArray);
    });
}

 

addActivity(contactNameSelected, username, function(response) {
    if(response[0])
    {                                
        ...
    }                                
    else
    {                                
        ...
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

thanks so much! that fixed the problem immediately. forgot about it being asynchronous

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.