Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why does node stopped when sending query to database #4540

Closed
2 tasks done
acteiongithub opened this issue Jan 30, 2025 · 2 comments
Closed
2 tasks done

Why does node stopped when sending query to database #4540

acteiongithub opened this issue Jan 30, 2025 · 2 comments
Labels

Comments

@acteiongithub
Copy link

acteiongithub commented Jan 30, 2025

Node.js Version

v22.12.0

NPM Version

10.9.0

Operating System

Windows 11

Subsystem

querystring

Description

* In the below API post method, before this db.query(surveyQuery, surveyValues, (err, surveyResult) => {, all the logs are executed.
* After no code is executed.
* Survey record is created in the database and stopped.
*

app.post('/savesurveyform', (req, res) => {
  console.log('BE - RES BODY:', req.body);

  const { surveyData, questions } = req.body;
  if (!surveyData || !Array.isArray(questions)) {
    return res.status(400).json({ message: 'Invalid request payload. Survey data or questions missing.' });
  }

  console.log('BE - SURVEYDATA:', surveyData);
  console.log('BE - QUESTIONSDATA:', questions);

  const { name, description, description_at_end, thank_you_message } = surveyData;

  const surveyQuery = `
      INSERT INTO surveys (name, description, description_at_end, thank_you_message)
      VALUES (?, ?, ?, ?)
  `;

  const surveyValues = [name, description || null, description_at_end || null, thank_you_message || null];

  console.log('Executing Survey Query:', surveyQuery, 'with Values:', surveyValues);

  db.query( surveyQuery, surveyValues, (err, surveyResult) => {
      console.log('Survey Insert Result:', surveyResult);
      if (err) {
          console.error('Error inserting survey:', err);
          res.status(500).send({ message: 'Error saving survey.' });
          return;
      }
      const surveyId = surveyResult.insertId;
      console.log('CREATED SURVEY ID:', surveyId);
      const questionQueries = questions.map(child => {
        const questionQuery = `
            INSERT INTO survey_questions (parent_id, Question_Name, Type, Minimum, Maximum, Min_Value_Word, Max_Value_Word, Choices, Pick_Values_with_Additional_Comments, Collect_Comments, isrequired, Rating_Type)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        `;
        const questionValues = [
            surveyId ?? null, 
            child.Question_Name || null,
            child.Type || null,
            child.Minimum && child.Minimum !== '' ? parseInt(child.Minimum) : null,
            child.Maximum && child.Maximum !== '' ? parseInt(child.Maximum) : null,
            child.Min_Value_Word || null,
            child.Max_Value_Word || null,
            child.Choices || null,
            child.Pick_Values_with_Additional_Comments || null,
            child.Collect_Comments ? 1 : 0,
            child.isrequired ? 1 : 0,
            child.Rating_Type || null,
        ];
        return new Promise((resolve, reject) => {
            db.query(questionQuery, questionValues, (error, result) => {
            if (error) return reject(error);
            resolve(result);
            });
        });
      });
  
      Promise.all(questionQueries)
      .then(() => {
          res.send({ message: 'Survey created successfully' });
      })
      .catch(error => {
          console.error('Error inserting survey questions:', error);
          res.status(500).send({ message: 'Failed to create survey questions' });
      });
    }
  );
});

Minimal Reproduction

No response

Output

No response

Before You Submit

  • I have looked for issues that already exist before submitting this
  • My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask
@avivkeller
Copy link
Member

avivkeller commented Mar 20, 2025

Hi! It's kinda hard to reproduce the issue from this small snippet which performs a lot of different tasks. Can you provide some example code that can be easily ran? i.e. a test script?

@avivkeller
Copy link
Member

No response from OP

@avivkeller avivkeller closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants