Particle JS API + Promises + AdonisJS Issues

Greetings,

I am deving an application with a Node/Express framework called AdonisJS. At this point I have successfully been able to do the authentication steps using the following:

* login(request, response){
	const email = request.email
	const pass 	= request.password

	particle.login({username: 'clayton******@gm******.com', password: '****'})
	.then(
		function(data){
			console.log('API call completed on promise resolve: ', data.body.access_token)
			response.cookie('token', data.body.access_token)
			response.redirect('/devices')
		},
		function(err){
			console.log(err)
		})
}

This works just fine to set the cookie value to store the token from Particle. Now when I use the following to go the view and list devices:

'use strict'

const Particle = require('particle-api-js')
const particle = new Particle();

class DeviceController {
* index(request, response){

	const token = request.cookie('token')

	const devicesPr = particle.listDevices({ auth: token })

	devicesPr.then(
		function(devices){
			//console.log('Devices: ', devices)
			response.send(devices)    			
		},
		function(err) {
			console.log('List devices call failed: ', err);
		})
   }
}

module.exports = DeviceController

I can successfully render in JSON all three of my Photon devices. But the problem arises when I try to render the actual view for the devices which in built in a template engine called Nunchucks. The part I don’t think I’m doing correctly is the response. The Adonis framework calls for the following code to render a view:

yield response.sendView('devices')

This I can do just fine outside of the Particle promise displayed above. But if I wanted to pass:

yield response.sendView('devices', devices)

This throws an error stating that devices has not been defined. However, I cannot do this inside of the promise because:

yield response.sendView('devices', devices)

will throw an unexpected token error (I think yield or sendView is throwing it off). Any help here?

Really?

Not one reply! Did I format this question poorly or something?

It’s an international community. Your question was posted a mere six hours ago. Not everyone (with the knowledge of what you’re asking) might be online currently.

Additionally, seeing your actual code might be beneficial, especially since you’re using multiple frameworks that not everyone might be familiar with. Not to mention actual error output would be helpful.
That might also have something to do with why no one has responded yet. Apart from that, give it some time, this forum is made up of volunteers. As such, any help is time invested by someone who might not get anything in return.

4 Likes