Cloud API Troubleshooting -- So Close -- Publish Event [Solved]

Greetings,

I am working on a web application using the MEAN Stack. At this point I have succeeded in logging in and setting a token in session storage and also listing my devices on a separate page. Now the third page is giving me some trouble.

For clarification purposes this application is using Angular and Node to perform the critical parts…

So in the view that I need to publish a function I am not using a form. In fact, I am only using two buttons as such:

<button class="btn btn-primary" ng-click="klaxonOn()">Klaxon On</button>
<button class="btn btn-danger" ng-click="klaxonOff()">Klaxon Off</button>

This calls a function inside my controller:

(function(){

angular
	.module('klaxon')
	.controller('klaxonCtrl', klaxonCtrl);

klaxonCtrl.$inject = ['$scope', 'dataservice', '$window'];

function klaxonCtrl($scope, dataservice, $window){

	$scope.klaxonOn = function(){
		dataservice.klaxonOn(window.sessionStorage.getItem('token'))
		.then(function success(res){
			console.log("klaxonOn");
			console.log(res);
		},function error(err){
			console.log(err);
		});
	}

	$scope.klaxonOff = function(){
		dataservice.klaxonOff(window.sessionStorage.getItem('token'))
		.then(function success(res){
			console.log("klaxonOff");
			console.log(res);
		},function error(err){
			console.log(err);
		});
	}

}

})();

As you may have spotted this gets sent to a dataservice as follows:

(function(){

angular
.module('klaxon')
.service('dataservice', dataservice);

dataservice.$inject = ['$http'];

function dataservice($http){

	this.login = function(username, password){

		var data = {
			username: username,
			password: password
		};

		return $http.post('/api/login', data);

	};

	this.listDevices = function(token){

		return $http.get('/api/getdevices/' + token);
		
	};

	this.klaxonOn = function(token){

		return $http.post('/api/klaxonOn/' + token);

	};

	this.klaxonOff = function(token){

		return $http.post('/api/klaxonOff/'+ token);

	};
}

})();

The last two functions are the only two of concern at this point. So from here I pass my token from this being on the client side to the API of my application in which I have literally pasted the code generated by postman which was successful at publishing an event to the console logs in particle. For the sake of brevity I will only post one of those as they are both the same with the exception of the data being “on” or “off” :

var qs = require("querystring");
var http = require("https");

module.exports.klaxonOn = function(req, res){

var token = req.params.token;

var options = {
"method": "POST",
"hostname": "api.particle.io",
"port": null,
"path": "/v1/devices/events?access_token=" + token,
"headers": {
  "content-type": "application/x-www-form-urlencoded",
  "cache-control": "no-cache",
}
};

var req = http.request(options, function (resp) {
var chunks = [];

resp.on("data", function (chunk) {
  chunks.push(chunk);
});

resp.on("end", function () {
  var body = Buffer.concat(chunks);
  console.log(body.toString());
  res.status(200).json({'success': true});

   });
 });

req.write(qs.stringify({ name: 'klaxon', data: 'on' }));
req.end();

};

Please bear in mind this worked in postman but for some reason, not quite publishing the events from my web application. Any thoughts? Help?

Cheers,

Holy cow guys!

I figured out what it was. I forgot to update that actual file where the API gets tied to a controller! After a whole day!!!

Here it goes:

var express = require('express');
var api = express.Router();


var loginCtrl = require('../controllers/loginController');
var devicesCtrl = require('../controllers/devicesController');
var klaxonCtrl = require('../controllers/klaxonController');

api.post('/login', loginCtrl.login);
api.get('/getdevices/:token', devicesCtrl.getdevices);

api.post('/klaxonOn/:token', klaxonCtrl.klaxonOn);
api.post('/klaxonOff/:token', klaxonCtrl.klaxonOff);

module.exports = api;

Good lord! I hope this might help someone else and save a lot of time!

Cheers!

2 Likes

Thanks for posting your solution! It’s always helpful when this is done. Also glad you were able to get it fixed!