OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Internal Server Error on javascript,sql, node.js

  • Thread starter Thread starter user20379594
  • Start date Start date
U

user20379594

Guest
So I have this server.js that has this function for searching the whole database so that I can use it to my front end for searching the data base this is the server function that causing the error

Code:
app.get('/search', (req, res) => {
    const searchTerm = req.query.q;

    if (!searchTerm) {
        return res.status(400).json({ error: 'Search term is required' });
    }

    const query = `
        SELECT * FROM (
            SELECT 'employee' AS type, employee_id AS id, name, position
            FROM employees
            WHERE MATCH(name) AGAINST(? IN BOOLEAN MODE)
            UNION ALL
            SELECT 'department' AS type, department_id AS id, name, manager
            FROM departments
            WHERE MATCH(name) AGAINST(? IN BOOLEAN MODE)
            UNION ALL
            SELECT 'project' AS type, project_id AS id, name, start_date, end_date
            FROM projects
            WHERE MATCH(name) AGAINST(? IN BOOLEAN MODE)
            UNION ALL
            SELECT 'task' AS type, task_id AS id, description, status
            FROM tasks
            WHERE MATCH(description) AGAINST(? IN BOOLEAN MODE)
        ) AS search_results
    `;

    const values = [searchTerm, searchTerm, searchTerm, searchTerm];

    connection.query(query, values, (err, results) => {
        if (err) {
            console.error('Error searching:', err);
            return res.status(500).json({ error: 'Failed to execute search' });
        }

        res.json(results); // Send search results as JSON response
    });
});

and this is the function for the client side

Code:
function searchData() {
        let searchInputValue = document.getElementById('searchInput').value.trim();
    
        if (searchInputValue !== '') {
            const encodedSearchTerm = encodeURIComponent(searchInputValue);
    
            fetch(`http://localhost:3000/search?q=${encodedSearchTerm}`)
                .then(response => {
                    if (!response.ok) {
                        throw new Error('Network response was not ok ' + response.statusText);
                    }
                    return response.json();
                })
                .then(data => {
                    console.log('Search results:', data);
                    const resultsContainer = document.getElementById('searchResults');
                    if (resultsContainer) {
                        resultsContainer.innerHTML = ''; // Clear previous results
                        data.forEach(item => {
                            const listItem = document.createElement('li');
                            if (item.type === 'employee') {
                                listItem.textContent = `Employee: ${item.name}, Position: ${item.position}`;
                            } else if (item.type === 'department') {
                                listItem.textContent = `Department: ${item.name}, Manager: ${item.manager}`;
                            } else if (item.type === 'project') {
                                listItem.textContent = `Project: ${item.name}, Start Date: ${item.start_date}, End Date: ${item.end_date}`;
                            } else if (item.type === 'task') {
                                listItem.textContent = `Task: ${item.description}, Status: ${item.status}`;
                            }
                            resultsContainer.appendChild(listItem);
                        });
                    }
                })
                .catch(error => {
                    console.error('Error searching:', error);
                    alert('Failed to search. Please try again later.');
                });
        } else {
            alert('Please enter a search term.');
        }
    }

and this is the error GET http://localhost:3000/search?q=DogNigga 500 (Internal Server Error) searchData @ script.js:13 script.js:41 Error searching: Error: Network response was not ok Internal Server Error at script.js:16:31

I don't know what to do since the other CRUD works just fine but when I'm searching the whole db it doesn't work

<p>So I have this server.js that has this function for searching the whole database
so that I can use it to my front end for searching the data base
this is the server function that causing the error</p>
<pre><code>app.get('/search', (req, res) => {
const searchTerm = req.query.q;

if (!searchTerm) {
return res.status(400).json({ error: 'Search term is required' });
}

const query = `
SELECT * FROM (
SELECT 'employee' AS type, employee_id AS id, name, position
FROM employees
WHERE MATCH(name) AGAINST(? IN BOOLEAN MODE)
UNION ALL
SELECT 'department' AS type, department_id AS id, name, manager
FROM departments
WHERE MATCH(name) AGAINST(? IN BOOLEAN MODE)
UNION ALL
SELECT 'project' AS type, project_id AS id, name, start_date, end_date
FROM projects
WHERE MATCH(name) AGAINST(? IN BOOLEAN MODE)
UNION ALL
SELECT 'task' AS type, task_id AS id, description, status
FROM tasks
WHERE MATCH(description) AGAINST(? IN BOOLEAN MODE)
) AS search_results
`;

const values = [searchTerm, searchTerm, searchTerm, searchTerm];

connection.query(query, values, (err, results) => {
if (err) {
console.error('Error searching:', err);
return res.status(500).json({ error: 'Failed to execute search' });
}

res.json(results); // Send search results as JSON response
});
});
</code></pre>
<p>and this is the function for the client side</p>
<pre><code>function searchData() {
let searchInputValue = document.getElementById('searchInput').value.trim();

if (searchInputValue !== '') {
const encodedSearchTerm = encodeURIComponent(searchInputValue);

fetch(`http://localhost:3000/search?q=${encodedSearchTerm}`)
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok ' + response.statusText);
}
return response.json();
})
.then(data => {
console.log('Search results:', data);
const resultsContainer = document.getElementById('searchResults');
if (resultsContainer) {
resultsContainer.innerHTML = ''; // Clear previous results
data.forEach(item => {
const listItem = document.createElement('li');
if (item.type === 'employee') {
listItem.textContent = `Employee: ${item.name}, Position: ${item.position}`;
} else if (item.type === 'department') {
listItem.textContent = `Department: ${item.name}, Manager: ${item.manager}`;
} else if (item.type === 'project') {
listItem.textContent = `Project: ${item.name}, Start Date: ${item.start_date}, End Date: ${item.end_date}`;
} else if (item.type === 'task') {
listItem.textContent = `Task: ${item.description}, Status: ${item.status}`;
}
resultsContainer.appendChild(listItem);
});
}
})
.catch(error => {
console.error('Error searching:', error);
alert('Failed to search. Please try again later.');
});
} else {
alert('Please enter a search term.');
}
}
</code></pre>
<p>and this is the error
GET http://localhost:3000/search?q=DogNigga 500 (Internal Server Error)
searchData @ script.js:13
script.js:41 Error searching: Error: Network response was not ok Internal Server Error
at script.js:16:31</p>
<p>I don't know what to do since the other CRUD works just fine but when I'm searching the whole db it doesn't work</p>
 

Latest posts

Top