简体   繁体   中英

phonegap app: Uncaught ReferenceError: require is not defined

I'm trying to connect to a remote database from my phonegap app.

I'm using an example script I found, but it's not working, as I keep getting require is not defined error.

Here's my code (it's inside a tag):

  var Client = require('mysql').Client;
  var client = new Client(); 
  client.host ='1**.**.**.**8:****';
  client.user = '*****';
  client.password = '*****************';
  console.log("connecting...");
  client.connect(function(err, results) {
      if (err) {
          console.log("ERROR: " + err.message);
          throw err;
      }
      console.log("connected.");
      clientConnected(client);
  });

  clientConnected = function(client)
  {
    tableHasData(client);
  }           


  tableHasData = function(client)
  {
      client.query(
          'SELECT * FROM test_db.Users LIMIT 0,10',
          // you can keep this function anonymous
          function (err, results, fields) {
              if (err) {
                  console.log("ERROR: " + err.message);
                  throw err;
              }
              console.log("Got "+results.length+" Rows:");
              for(var i in results){
                console.log(results[i]); 
                console.log('\n');

                //console.log("The meta data about the columns:");
                //console.log(fields);     
              }
              client.end();
          });
  };

What am I doing wrong??

You are probably using nodeJS code that can't be run on Cordova. This must be run with node .

What you need to do is create a server (where you will run your code with nodeJS) and expose your data through an API for the client (your Cordova app) to come and fetch them. (Use AJAX requests)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM