Error when finding the width of a Loaded Image

Code:

local function displayImage(event) local response = event.response local decodedResponse = json.decode(response) local imgUrl = decodedResponse.img currentImg = display.loadRemoteImage(imgUrl, "GET", imgLoaded, "currentImg.png", system.DocumentsDirectory, 0, 0) local imgWidth = currentImg.width currentImg.x = display.contentCenterX - (imgWidth / 2) end

Error:

Runtime error /Users/qsysmine/Library/Application Support/Outlaw/Sandbox/15/main.lua:19: attempt to index upvalue 'currentImg' (a nil value) stack traceback: [C]: ? /Users/qsysmine/Library/Application Support/Outlaw/Sandbox/15/main.lua:19: in function \</Users/qsysmine/Library/Application Support/Outlaw/Sandbox/15/main.lua:14\> Runtime error: /Users/qsysmine/Library/Application Support/Outlaw/Sandbox/15/main.lua:19: attempt to index upvalue 'currentImg' (a nil value) stack traceback: [C]: ? /Users/qsysmine/Library/Application Support/Outlaw/Sandbox/15/main.lua:19: in function \</Users/qsysmine/Library/Application Support/Outlaw/Sandbox/15/main.lua:14\>

display.loadRemoteImage() does not return a display object.  Its a wrapper around network.request() and you have to provide a call back function that will be called after the image is downloaded.  It will be known as event.target inside that function.  Since nothing is returned these two lines:
 

local imgWidth = currentImg.width
currentImg.x = display.contentCenterX - (imgWidth / 2)

have errors because currentImg is nil.

Rob

Thanks, Rob!

display.loadRemoteImage() does not return a display object.  Its a wrapper around network.request() and you have to provide a call back function that will be called after the image is downloaded.  It will be known as event.target inside that function.  Since nothing is returned these two lines:
 

local imgWidth = currentImg.width
currentImg.x = display.contentCenterX - (imgWidth / 2)

have errors because currentImg is nil.

Rob

Thanks, Rob!