Can't get return value from particle function in Javascript

Hi,

For a uni task, I have to connect some LEDs to an Argon and create a webpage with buttons to turn them on/off. I got to the point where I could open command prompt and run the below command. It works and command prompt outputs the correct return value.
curl https://api.particle.io/v1/devices/device_id/toggle_led \ -d access_token=token \ -d "args=blue"

After that I made the webpage with some javascript, folliowing the callFunction part of this document. I can turn the LEDs on and off but I can’t get the return value. The document says that data.return_value is the returned value, but for me it’s undefined when I try to log it to the console.

Here is my javascript code. I’d appreciate any help. I had var Particle = require('particle-api-js'); at the top of the function (cos it’s in the doc) but the console said ‘require is not defined’, so I removed it.

function toggle_led(colour)
{
    var particle = new Particle();
    var function_ptr = particle.callFunction({ deviceId: 'device_id', name: 'toggle_led', argument: colour, auth: 'auth'});

    function_ptr.then(
        function(data)
        {
            console.log('Function called succesfully:', data.return_value);
        },
        function(error)
        {
            console.log('An error occurred:', error);
        }
    );
}

It’s data.body.return_value.

particle.callFunction({ deviceId, name: functionName, argument: arg, auth: apiHelper.auth.access_token  }).then(
	function (data) {
		setStatus('Success! (' + data.body.return_value + ')');
		setTimeout(function() {
			setStatus('');
		}, 4000);                
	},
	function (err) {
		setStatus('Error: ' + err);
		setTimeout(function() {
			setStatus('');
		}, 10000);                
	}
);            

Thanks very much. I’m getting the return value now.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.