Select and Insert to MySql database with node.js on remote server from Ionic2 app


#1

Hello,

I’m trying to find some useful and complete example, how to properly access, select and insert from ionic2 app to MySql database with nodejs on remote server.

Advice would be helpful


#2

hi @lado

try below code:
create new provider request

$ ionic g provider request

import { Injectable } from '@angular/core';
// import { Http } from '@angular/http';

import { Http , Headers, RequestOptions} from '@angular/http';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';


@Injectable()
export class RequestProvider {

  hostname:string = "http://192.168.0.55:3000";
  constructor(public http: Http) {
    console.log('Hello RequestProvider Provider');
  }
  get(endPoint){
    let url = this.hostname + endPoint;
    var promise = new Promise((resolve,reject)=>{
        this.http.get(url).map((res)=> res.json())
        .subscribe(data => {
          resolve(data);
        })
    })
    return promise
  }
  post(endPoint,data){
    let url = this.hostname + endPoint;
    let headers = new Headers();
    headers.append('Access-Control-Allow-Origin' , '*');
    headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
    headers.append('Accept','application/json');
    headers.append('content-type','application/json');
      let options = new RequestOptions({ headers:headers});
    return new Promise((resolve,reject)=>{
       this.http.post(url,JSON.stringify(data), options).subscribe(res => {
          resolve(res.json());
        }, (err) => {
          reject(err);
        });
    })
  }
}

and use this provider in your ionic application
thanks


#3

Hello, addwebsolution! Thanks for answer


#4

you’re welcome :smile:


#5

@lado

if your issue resolved please mark as a solution

thanks


#6

Where to insert the mysql query?


#7

The code in @addwebsolution’s post is an absolute trainwreck and no part of it should be emulated by anybody.