Dano007
Dano007

Reputation: 1932

Javascript error: Uncaught TypeError: Object [object Object] has no method 'src'

I'm using the following code to take images from a parse.com class and return them to the page inserted within a div.

At the moment I get a Uncaught TypeError: Object [object Object] has no method 'src'

and no images are being returned.

The images are stored in the class Gbadges in parse.com and as a string (URL) in the column.

I cannot find an complete match on SO or google to this issue. I presume its something to do with the image url?

Please note that this code is is based on backbone.js framework, which lets you ebed script tags into your html 5 code.

I've created a fiddle here http://jsfiddle.net/Dano007/cQgJG/

<!doctype html>
<head>
  <meta charset="utf-8">

  <title>My Parse App</title>
  <meta name="description" content="My Parse App">
  <meta name="viewport" content="width=device-width">
  <link rel="stylesheet" href="css/reset.css">
  <link rel="stylesheet" href="css/styles.css">
  <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
  <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
  <script type="text/javascript" src="http://www.parsecdn.com/js/parse-1.2.17.min.js"></script>
</head>

<body>

  <div id="main">
    <h1>You're ready to use Parse!</h1>

    <p>Read the documentation and start building your JavaScript app:</p>

    <ul>
      <li><a href="https://www.parse.com/docs/js_guide">Parse JavaScript Guide</a></li>
      <li><a href="https://www.parse.com/docs/js">Parse JavaScript API Documentation</a></li>
    </ul>

    <div style="display:none" class="error">
      Looks like there was a problem saving the test object. Make sure you've set your application ID and javascript key correctly in the call to <code>Parse.initialize</code> in this file.
    </div>

    <div style="display:none" class="success">
      <p>We've also just created your first object using the following code:</p>

        <code>
          var TestObject = Parse.Object.extend("TestObject");<br/>
          var testObject = new TestObject();<br/>
          testObject.save({foo: "bar"});
        </code>
    </div>
  </div>

  <script type="text/javascript">
    Parse.initialize("79tphN5KrDXdjJnAmehgBHgOjgE2dLGTvEPR9pEJ", "9lblofQNZlypAtveU4i4IzEpaOqtBgMcmuU1AE6Y");

    var TestObject = Parse.Object.extend("TestObject");
    var testObject = new TestObject();
      testObject.save({foo: "bar"}, {
      success: function(object) {
        $(".success").show();
      },
      error: function(model, error) {
        $(".error").show();
      }

    });

var GlobalBadges = Parse.Object.extend("GBadges");
    var query = new Parse.Query(GlobalBadges);
    query.exists("Global_Badge_Name");
    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('Global_Badge_Name'));
        }

        $('#Image01').src(imageURLs[0]); //first image
        $('#Image02').src(imageURLs[1]); //second image
        $('#Image03').src(imageURLs[2]); //third image
      },
      error: function(error) {
        // If the query is unsuccessful, report any errors
        alert("Error: " + error.code + " " + error.message);
      }
    });

 </script>

<div >
<img id="Image01"/>
<img id="Image02"/>
<img id="Image03"/>
 </div>


</body>
</html>



</body>
</html>

Upvotes: 0

Views: 639

Answers (2)

Arun P Johny
Arun P Johny

Reputation: 388316

You need to use attr() to set the value of src attribute.

$('#Image01').attr('src',imageURLs[0]);

Upvotes: 3

Cjmarkham
Cjmarkham

Reputation: 9681

It is because the javascript is before the elements. Place the javascript below the elements (Image elements) and it should work fine.

Upvotes: 0

Related Questions