0

I am getting a ReferenceError: serializeProducts is not defined error in my console and not sure why.

  • I have a Javascript file called components.js that contains a function called serializeProducts.
  • A template calls this components.js script and then a script below uses the serializeProducts function.

        <script type="text/javascript" src="{{STATIC_URL}}js/components.js"></script>
        <script type="text/javascript">
         if($("#parsed_found").val() == undefined)
            items_found = ""
        else
            items_found = serializeProducts($("#parsed_found").val().split("\n"));
    
        if($("#parsed_not_found").val() == undefined)
            items_not_found = ""
        else
            items_not_found = serializeProductsNotFound($("#parsed_not_found").val().split("\n"));
    

    Notes

  • components.js is definitely being found properly as I can access through console, 200 status, etc.

  • I am not getting the error when running the script locally, but when I load the components.js file from Amazon S3 this is when it seems to not work, which I am having trouble figuring out as the permissions are public.

I am wondering if this could be an issue with the components.js script not loading fully before the script on the page runs? I've tried adding a delay, but still am getting the error. Any other ideas?

components.js: http://snapeda.s3.amazonaws.com/js/components.js

2

1 Answer 1

1

Since you're already using JQuery, and have some code in your page depending on an external component, I'd really wrap that inside a $(document).ready(function() { ... });, just to make sure that everything is nice and dandy before executing instructions.

A different option would be putting that code in the same components.js script, but I don't know if that would be feasible for your case.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you- I just tried wrapping it inside this but no luck still. Still the same error.
Just to be on the safe side, then, try using $(window).load instead of $(document).ready: it still might be just a timing issue.
Ok that worked! Now I have what appears to be another timing issue in another file though. I am getting TypeError: undefined is not a function. Do you think that is also timing related? Again, this works when I"m running from my files locally. Would it make sense to post a separate question about this one?
That more likely means you're trying to use something that doesn't exist as a function: doesn't it tell you where the error happens?
Actually I figured out a solution. I placed my script in a template that was inheriting my current template. That gave the script enough time to load before the next script ran and solved all my problems.

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.