JavaScript

时间:2019-03-04 21:18:00

标签: javascript node.js

我一直在努力争取这个if语句,现在已经工作了太多小时了。它告诉我,第9行上的')'是令牌错误。任何建议的修补程序都将很棒!我直接从正在观看的视频中复制了它,但仍然无法正常工作。

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mysql = require('mysql');

var connect = mysql.createConnection({
  host: 'localhost',
  user: 'root',
  password: '',
  database: 'db'
});

connection.connect(function (error) {
  // callback
  if (!!error) {
    console.log('Error');
  } else {
    console.log('Connected');
  }
});

app.get('/', function (req, resp) {
  connection.query("Select * from db", function (error, rows, field) {
    if (!!error) {
      console.log('Error');
    } else {
      console.log('Succesful');
    }
  });
})

app.listen(3306);
console.log('Running on Port 3000')

1 个答案:

答案 0 :(得分:0)

您的var connect = mysql.createConnection({…是错误的,因为您在代码中使用的变量是connection而不是connect

还要注意,用于检查!!中的错误的双if是胡说八道。

必须是:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var mysql = require('mysql');

var connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'db'
});

connection.connect(function (error) {
    // callback
    if (error) {
        console.log('Error');
    } else {
        console.log('Connected');
    }
});

app.get('/', function (req, resp) {
    connection.query("Select * from db", function (error, rows, field) {
        if (error) {
            console.log('Error');
        } else {
            console.log('Successful');
        }
    });
})

app.listen(3306);
console.log('Running on Port 3000')