0
mysql.query(`SELECT templateCode from templates WHERE templateCode = "${templateCode}"`, (err, result) => {
            if (result.length !== 0) {
                req.session.success = false;
                req.session.message = "Template Code must be unique!";
                return res.redirect("/giftCard/templates");
            }
            mysql.query(
                `INSERT INTO outertemplateimages (outerImagePath, outerImageUrl) VALUES("${outerImageFilePath}", "${process.env.URL}${outerImageFilePath}")`,
                async (err, { insertId: outerImageId }) => {
                    mysql.query(
                        `INSERT INTO innertemplateimages (innerImagePath, innerImageUrl) VALUES("${innerImageFilePath}", "${process.env.URL}${innerImageFilePath}")`,
                        async (err, { insertId: innerImageId }) => {
                            mysql.query(
                                `INSERT INTO templates (templateName, templateCode, outerImage, innerImage) VALUES("${templateName}", "${templateCode}", ${outerImageId}, ${innerImageId})`,
                                (err, result) => {
                                    if (!err) {
                                        req.session.success = true;
                                        req.session.message = "New Template is added successfully";
                                        res.redirect("/giftCard/templates");
                                    }
                                }
                            );
                        }
                    );
                }
            );
        });

As the title mentioned, is there any way to write non-nested SQL query in node.js? What the query return is not the result. Anyone has the idea on it?

0

1 Answer 1

1

By using a node module that supports a Promise API for MySQL.

Or by not nesting the callbacks, and using named functions instead:

function(req, res, next) {

  mysql.query(`SELECT templateCode from templates WHERE templateCode = "${templateCode}"`, processTemplateCodes);

  function processTemplateCodes(err, result) {
    if (result.length !== 0) {
      req.session.success = false;
      req.session.message = "Template Code must be unique!";
      return res.redirect("/giftCard/templates");
    }
    mysql.query(
      `INSERT INTO outertemplateimages (outerImagePath, outerImageUrl) VALUES("${outerImageFilePath}", "${process.env.URL}${outerImageFilePath}")`,
       outerTemplateImageInserted);
  }


  function outerTemplateImageInserted(err, {
    insertId: outerImageId
  }) {
    mysql.query(
      `INSERT INTO innertemplateimages (innerImagePath, innerImageUrl) VALUES("${innerImageFilePath}", "${process.env.URL}${innerImageFilePath}")`,
      innerTemplateImageInserted);
  }

  function innerTemplateImageInserted(err, {
    insertId: innerImageId
  }) {
    mysql.query(
      `INSERT INTO templates (templateName, templateCode, outerImage, innerImage) VALUES("${templateName}", "${templateCode}", ${outerImageId}, ${innerImageId})`,
      (err, result) => {
        if (!err) {
          req.session.success = true;
          req.session.message = "New Template is added successfully";
          res.redirect("/giftCard/templates");
        }
      }
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.