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?