0

I have an array (currently with 1 item in) and this prints out as follows:

enter image description here

The console image is the result of console.log(objects) but I need the first item in that array so I am trying to select the object at index 0 but this is not working and is printing in the console as 'undefined' using the following code:

var objects = wpd_editor.canvas.getObjects();
console.log(objects[0]);
11
  • What does console.log(objects) log? Commented Nov 14, 2016 at 21:56
  • what? youre showing us a screenshot of the console NOT being empty, yet your question says the console is empty (shows undefined). Commented Nov 14, 2016 at 21:57
  • The console image is the result of console.log(objects) but I need the first item in that array - sorry for not being clear Commented Nov 14, 2016 at 21:58
  • Please try this : console.log(objects['u']) Commented Nov 14, 2016 at 21:59
  • Thanks @AlexandruMihai but it still says 'Undefined' Commented Nov 14, 2016 at 22:00

3 Answers 3

1

Based on the answer from @shanzilla, you likely need to wait until everything is loaded—including your Fabric.js module and any drawing you may be doing—before trying to check if something exists. Not sure on how you're loading the Fabric.js code or when you're drawing the canvas, but a quick & dirty way to check if this is the issue is to wrap that log in a timeout:

setTimeout(function () {
  var objects = wpd_editor.canvas.getObjects();
  console.log(objects[0]);
}, 1000)

This is likely not a sustainable solution for you, but it will let you know that, in that one second that you're waiting, some crucial things are happening, specifically the loading and rendering of the page. For a more sustainable solution, something like the following, which uses Fabric.js's events API to wait for the canvas to be rendered, would probably work:

wpd_editor.canvas.on('after:render', function() {
  var objects = wpd_editor.canvas.getObjects();
  console.log(objects[0]);
});
Sign up to request clarification or add additional context in comments.

Comments

0

It seems likely that you're trying to log the first element of the objects array before its defined.

Try...

if (objects.length > 0)
   console.log(objects[0])

If nothing is output then it's a timing issue.

2 Comments

Nothing is outputted - what I don't understand however is why it outputs when I just do console.log(objects)
@AppleTattooGuy The console log is a dynamic reference to the array. If you add something to the array after calling console.log(), it will show up when you expand the object.
0

Something like this should bring you closer to the solution:

for(var item of objects)
    console.log(item)

or

for(var i = 0; i < objects.length; i++)
    console.log(objects[i])
// if this one works, you should check if you accessing the array at the right time.

3 Comments

Neither of those do anything i'm afraid
but console.log(objects) directly outputs any items in it?
Yes, that outputs as per the screenshot - I need to only select the first item from it though

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.