How do I fetch SQL server data on my barcodescanning app?

#1

Good day guys!

I’ve build a barcode scanning app which scans barcode and show the numbers result.
using this https://github.com/adamweeks/ion-scanner

Now i want to connect it to a sql server and fetch a data
For example a product data . How do i do that?

Will you suggest an REST API? how do implement it. Sorry I’m new to this.

Thank you:)

#2

Yep, you’d need to setup an API for this. In your app, you could create an API/Backend service, kinda like the following:

import { Http, Headers, RequestOptions, RequestMethod, URLSearchParams, Response } from '@angular/http';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/timeout';

...

constructor(
	private http: Http
) {}

post(params: Object, body: Object, timeout = 60 * 1000) {
	// Build your url using the params, if any
	let url = "http://api.domain.com/v1/";

	// Headers - Add the ones you need, if any
	let headers = {};

	// Request options
	let options = new RequestOptions({ headers: new Headers(headers) });

	// Return the Request Observable
	return this.http.post(url, JSON.stringify(body), options)
		.timeout(timeout)
		.map((res: Response) => res.json());
}

Ish. Probably needs some tweaking to suit your needs.

Edit: My apologies, I just realized you tagged this as Ionic and not Ionic 2. In that case, I’m sure you can find your way on Google to the equivalent of what I said :wink:

#3

ok, so i’ve already setup my Rest api to connect to SQL server

I used Express for this.

Here’s the code for the barcode scanner

angular.module(‘scanner.controllers’, [])
.controller(‘HomeController’, function($scope, $rootScope, $cordovaBarcodeScanner, $ionicPlatform, $http, $ionicLoading, $ionicPopup, $timeout) {
var vm = this;

    vm.scan = function(){
      getInformation();
    };

    beginScan = function() {
      $ionicPlatform.ready(function() {
          $cordovaBarcodeScanner
              .scan()
              .then(function(result) {
                  // Success! Barcode data is here
                  $scope.scanResults = "Barcode: " + result.text + "<br>" +
                                       "Format: " + result.format + "<br>";
                  getInformation();
              }, function(error) {
                  // An error occurred
                  $scope.scanResults = 'Error: ' + error;
              });
      });
    };

My barcode scanner gets the barcode number now

Now how do i request an id or data on json

then get the specific details??