如何单独创建DBconnection和Node.js查询文件

时间:2018-06-16 15:20:51

标签: javascript mysql sql json node.js

我有疑问。如何连接dbconnection.js和demo_api_select.js我在demo_api_select.js上声明变量时遇到问题。它没有工作,并且有这样的错误通知:

Error Notification

请帮忙

  

dbconnection.js:

    ConstraintLayout layout = findViewById(R.id.constLayout);
    ImageView player = findViewById(R.id.player);
    ImageView target = findViewById(R.id.tile1_3);

    ConstraintSet constraintSet = new ConstraintSet();
    constraintSet.clone(layout);
    constraintSet.connect(player.getId(), ConstraintSet.START, target.getId(), ConstraintSet.START);
    constraintSet.connect(player.getId(), ConstraintSet.END, target.getId(), ConstraintSet.END);
    constraintSet.connect(player.getId(), ConstraintSet.TOP, target.getId(), ConstraintSet.TOP);
    constraintSet.connect(player.getId(), ConstraintSet.BOTTOM, target.getId(), ConstraintSet.BOTTOM);
    constraintSet.applyTo(layout);
  

demo_api_select.js

var mysql=require('mysql');

var con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: "",
  database: "test"
});

con.connect(function(err) {
  if (err) throw err;
  console.log("Connected!");
});


module.exports = con;

我想让这些代码与这段代码一样有效:

var db = require('./dbconnection');

db.connect(function(err) {
  if (err) throw err;
  //Select all customers and return the result object:
  con.query("SELECT * FROM profil" , function (err, result, fields) {
    if (err) throw err;
    console.log(result);
  });
});

1 个答案:

答案 0 :(得分:0)

尝试此代码。无需连接两次到数据库

var db = require('./dbconnection');

db.query("SELECT * FROM profil" , function (err, result, fields) {
   if (err) throw err;
    console.log(result);
   });
});