我试图了解为什么调试器不进入回调函数,它不给出任何错误或任何结果……它只是跳过connect.query!

当我在mySQL工作台中运行时,代码会运行并得到数据,因此查询也不正确。不会进入回调)

SELECT * FROM eclasstree WHERE numberofdigits = '8';


这是不输入回调的代码部分,上面的所有其他SQL查询都可以正常工作...

connection.query("SELECT * FROM eclasstree WHERE numberofdigits = '8'", function(error, results, fields) {
                if (error) {
                  throw error;
                }
                console.log(results, fields);
              });


index.js //路由/ generatewordsconnectedtoeclass

require("console-stamp")(console, "[HH:MM:ss.l]");
const express = require("express");
const router = express.Router();
const { Database, mySQLConnectionDetails, connection } = require("../helpers/mySQL");
const { cleanseString, cleanseStringToArray, generateCountDuplication, sortValuesBykey, generateScoreArray, calculateAverage, replaceAbbreviationWithExpansion } = require("../helpers/stringFixers");

const database = new Database(mySQLConnectionDetails);


router.get("/generatewordsconnectedtoeclass", (req, res) => {
  const eClassArray = [];
  let eClassCode = "";

  connection.query("SELECT * FROM eclasstree WHERE numberofdigits = '8'", function(error, results, fields) {
    results.forEach(eclassobj => {
      eClassCode = eclassobj.code;

      connection.query(`SELECT itemnumber, cleanname, eclass FROM partnumbersclassified WHERE eclass = '${eclassobj.code}'`, function(error, results, fields) {
        let stringToArray = [];
        let countObject = [];

        if(results.length > 0) {
          stringToArray = cleanseStringToArray(results, "cleanname");
        }

        if(stringToArray.length > 0) {
          countObject = generateCountDuplication(stringToArray);
          // console.log(countObject);
          for (const property in countObject) {
            if (countObject.hasOwnProperty(property)) {
              // console.log(property + " : " + countObject[property] + " : " + eClassCode);
              // console.log(property);
              // const obj = {
              //  eclasscode: `${eClassCode}`,
              //  wordcount: `${countObject[property]}`
              // };
              // obj[property] = `${countObject[property]}`;
              // const obj2 = obj;
              //              connection.query(`INSERT INTO wordsconnectedtoeclasscode (eclasscode, word, wordcount) VALUES (${eClassCode}, ${property}, ${countObject[property]})`, function(error, results, fields) {
              // const post = {word: "Hello", wordcount: "1"};
              // connection.connect();
              connection.query("SELECT * FROM eclasstree WHERE numberofdigits = '8'", function(error, results, fields) {
                if (error) {
                  throw error;
                }
                console.log(results, fields);
              });
              // connection.query("INSERT INTO posts SET ?", post, function(error, results, fields) {
              // //connection.query("INSERT INTO wordsconnectedtoeclasscode (eclasscode, word, wordcount) VALUES ('12345123', 'balle', '2'})", function(error, results, fields) {
              //   if (error) throw error;
              //   console.log(fields);
              //   console.log(results);
              //   connection.end();
              // });
            }
          }
        }
      });
    });
  });
  res.json({});
});

module.exports = router;


example

如果执行“逐步”,则可以看到查询已定义了“ sql”,但未定义“ cb”!
javascript - 如何调试不会产生任何错误的代码或输入回调?-LMLPHP

最佳答案

在@Patrick Evans的帮助下,我解决了它。

我阅读了有关mysql节点程序包的文档,但它们没有遵循标准的mysql查询。因此,在我对其进行更改之后,它就起作用了!

const obj = {
                eclasscode: `${eclassobj.code}`,
                wordcount: `${countObject[property]}`,
                word: property
              };
              connection.query("INSERT INTO wordsconnectedtoeclasscode SET ?", obj, function(error, results3) {
                if (error) {
                  throw error;
                }
                console.log(results3.insertId);
              });




require("console-stamp")(console, "[HH:MM:ss.l]");
const express = require("express");
const router = express.Router();
const { Database, mySQLConnectionDetails, connection } = require("../helpers/mySQL");
const { cleanseString, cleanseStringToArray, generateCountDuplication, sortValuesBykey, generateScoreArray, calculateAverage, replaceAbbreviationWithExpansion } = require("../helpers/stringFixers");

const database = new Database(mySQLConnectionDetails);



router.get("/generatewordsconnectedtoeclass", (req, res) => {

  connection.query("SELECT * FROM eclasstree WHERE numberofdigits = '8'", function(error, results1) {
    results1.forEach(eclassobj => {
      connection.query(`SELECT itemnumber, cleanname, eclass FROM partnumbersclassified WHERE eclass = '${eclassobj.code}'`, function(error, results2) {
        let stringToArray = [];
        let countObject = [];

        if(results2.length > 0) {
          stringToArray = cleanseStringToArray(results2, "cleanname");
        }

        if(stringToArray.length > 0) {
          countObject = generateCountDuplication(stringToArray);
          for (const property in countObject) {
            if (countObject.hasOwnProperty(property)) {

              const obj = {
                eclasscode: `${eclassobj.code}`,
                wordcount: `${countObject[property]}`,
                word: property
              };
              connection.query("INSERT INTO wordsconnectedtoeclasscode SET ?", obj, function(error, results3) {
                if (error) {
                  throw error;
                }
                console.log(results3.insertId);
              });
            }
          }
        }
      });
    });
    // res.json({});
  });
});

module.exports = router;

关于javascript - 如何调试不会产生任何错误的代码或输入回调?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55547020/

10-09 08:57