0

we are working on a university project and we are now facing this big problem. We are using React and Javascript and we need to get some data from a MYSQL database on 000webhost.com and simply store them on a Javascript variable. We searched a lot on the internet but we coudln't find any solution, probably just because we are missing something important or we don't understand something properly. This is the PHP file hosted on Webhost000 which works perfectly:

<?php
//Connect to DB
$servername = "localhost";
$username = "id1761679_nedo93";
$password = "*********";
$dbname = "id1761679_mrtdb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection

if ($conn->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}

//Name Array Selector
$queryN = "SELECT Name FROM persontable";
$name = mysqli_query($conn, $queryN) or die ("no query");

$name_array = array();
while($row = mysqli_fetch_assoc($name)) {
   $name_array[] = $row;
}   

$nameJSON = json_encode($name_array);

echo $nameJSON;

?>

This file's URL is https://nedo93.000webhostapp.com/phpgetcoords.php and in fact if you click on it, it should echo all names stored in database in a JSON format.

Now, we just need to pass this json variable to a javascript which is not running on Webhost but is running in a Node terminal on a PC.

We tried AJAX and everything we could find on Google and Youtube but nothing seems to work, we are quite desperate.

Even if it shouldn't be relevant for the question, this is the Javascript file on which we need to store the data in order to associate names and coordinates with a deck.gl layer and display them on a map. The only problem is to link the data from dabase with a variable in this file:

import React, {Component} from 'react';
import DeckGL, {PathLayer} from 'deck.gl';
import DeckGL, {ScatterplotLayer} from 'deck.gl';

var currPos = null;
var vname = null; 

//This is the function we are trying but it's not working, the variable vnam is empty.

(function () 
  {   
    $.ajax({                                      
      url: 'https://nedo93.000webhostapp.com/phpgetcoords.php',                          
      data: "",                                 
      dataType: 'json',                     
      success: function(data)          
      {                      
        vname = data[1];         
      } 
    });
  });




//Position tracker
navigator.geolocation.watchPosition(

function(position) {
    currPos = position;
},
   function(error){
       console.log(error);
   }
);

export default class DeckGLOverlay extends Component {  

  static get defaultViewport() {
    return {
      longitude: 11.20, //11.25,
      latitude: 43.77, //43.77,
      zoom: 16,
      maxZoom: 18,
      pitch: 40, //viewport tilt
      bearing: 0
    };
  }

  _initialize(gl) {
    gl.enable(gl.DEPTH_TEST);
    gl.depthFunc(gl.LEQUAL);
  }
    //Add in const { }, new layers's ID
  render() {
    const {viewport, scatters, pos, icon, paths, trailLength, time} = this.props;   

    //Add another instance of a new layer here:

    const layers = [      
      new ScatterplotLayer({
        id: 'scatters',
        data: scatters,     
        radiusScale: 100,
        outline: false
       }),
      new ScatterplotLayer({
        id: 'pos',
        data: pos,
        getPosition: a => [currPos.coords.longitude, currPos.coords.latitude, 1],
        radiusScale: 100,
        outline: false
       }),    
       new PathLayer({
        id: 'paths',
        data: paths,        
        rounded: true,
        getColor: paths => [255, 0, 0, 255],
        getWidth: paths => 0.03,
        widthScale: 100
      })
    ];  

    return (
      <DeckGL {...viewport} layers={layers} onWebGLInitialized= this._initialize} />
    );      
  }    
}

Thanks in advance to anybody who can help us.

  • 3
    "We tried AJAX and everything we could find on Google and Youtube but nothing seems to work" — If you show us the attempt, then we might be able to tell what went wrong with it. Writing yet-another-ajax tutorial isn't likely to be very productive. – Quentin Jun 01 '17 at 15:28
  • "Now, we just need to pass this json variable to a javascript which is not running on Webhost but is running in a Node terminal on a PC" — This is a rather confusing sentence. The JavaScript you've provided and the tags you put makes this question look like you are asking about browser-side JavaScript. Mentioning that your HTTP server is one you have written in JavaScript and are running on Node instead of one owned by a third party seems like a red herring that is just going to make people think you want to make the request from Node instead of from the browser. – Quentin Jun 01 '17 at 15:30
  • I'd be surprised if this didn't turn out to be a duplicate of https://stackoverflow.com/q/14220321/19068 or https://stackoverflow.com/a/35553666/19068 – Quentin Jun 01 '17 at 15:32
  • We are running that javascript on Node and we would like to make the request from that script, I don't know, we are quite newbie and we are trying to understand how to do it. – Edoardo Bianchi Jun 01 '17 at 15:37
  • It's React! You're trying to use `navigator.geolocation`! If you're **running** that JS on Node (rather than using Node to send it to the browser (probably after transpiling it to ES5) to be run there) then you are doing something wrong. – Quentin Jun 01 '17 at 15:38
  • "we would like to make the request from that script, I don't know, we are quite newbie and we are trying to understand how to do it" — Go and read an Ajax tutorial then. – Quentin Jun 01 '17 at 15:38
  • We tried to use an Ajax script to make the request, but when we try to launch the application with "npm start" after implementing the script, it will just give us errors. I'm gonna edit the question with the Ajax call we tried to work on. – Edoardo Bianchi Jun 01 '17 at 15:42
  • "We tried to use an Ajax script to make the request" — Good! Show us an [mcve]! – Quentin Jun 01 '17 at 15:43
  • "it will just give us errors" — Tell us what the errors are! – Quentin Jun 01 '17 at 15:43
  • I edited the javascript code with the function we are using at the moment, which is not giving us errors but it's not working. Maybe we just wrote something wrong. – Edoardo Bianchi Jun 01 '17 at 16:02
  • `var vname = data[1];` … so you assign it to a variable inside the success function, and then do nothing, so it drops out of scope and is garbage collected. – Quentin Jun 01 '17 at 16:03
  • Yes that's true, but even trying with a global variable, the variable is still empty. – Edoardo Bianchi Jun 01 '17 at 16:14
  • Which brings us back to https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call (as mentioned previously). – Quentin Jun 01 '17 at 16:15
  • Ok, we are going to take a good look at that question, thank you for your help and patience. – Edoardo Bianchi Jun 01 '17 at 16:21

0 Answers0