Hello,
Can someone give any ideas why this is happening, I've had many days reseraching and changing the code as suggested but it is not working; I received the following error:
“Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received”
Doing some research on, I found out that maybe is a cross-origin request issue and it is caused by various Chrome Extensions, but I checked and I do not have any Chrome extension blocking.
Thank you.
async function getUserDetails(MemberID) {
try {
const config = {
server: '.',
database: 'WebForms',
options: {
//encrypt: true // Use this if you're on Azure
}
};
// Connect to the database
await sql.connect(config);
// Query the database
const result = await sql.query`SELECT RTRIM(LTRIM(CONCAT(COALESCE(FirstName + ' ', ''), COALESCE(LastName, '')))) AS MemberName, SSN FROM dbo.Members WHERE MemberID = ${MemberID}`;
if (result.recordset.length > 0) {
const { MemberName, SSN } = result.recordset[0];
console.log(`Name: ${MemberName}, SSN: ${SSN}`);
document.getElementById("membername").value = MemberName;
document.getElementById("SSN").value = SSN;
} else {
console.log('No user found with the provided account number.');
document.getElementById("membername").value = '';
document.getElementById("SSN").value = '';
}
} catch (err) {
console.error('SQL error', err);
} finally {
await sql.close();
}
}
Jayraj ChhayaPosted Oct 22, 2024, 6:56 AM
The error message “Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received” typically indicates that a promise was not resolved or rejected properly. This can occur if the asynchronous operation (like your database query) fails or if there is an issue with the event listener that is supposed to handle the response.
In your code, ensure that the
sql.connect(config)andsql.querycalls are functioning correctly. If the connection fails or the query does not return a result, it may lead to this error. Additionally, check if there are any unhandled promise rejections in your code. You can add a.catch()to your promise chain to handle any errors gracefully.Here’s a modified version of your function with improved error handling:
Make sure to test your database connection and query separately to isolate the issue. If the problem persists, consider checking your browser's console for additional clues or errors.
Ivonne AspilcuetaPosted Oct 21, 2024, 6:36 PM
Hello Paul,
I wrapped the SQL query in a try-catch block, but it did not populate any error, the only error is the same:
“Uncaught (in promise) Error: A listener indicated an asynchronous response by returning true, but the message channel closed before a response was received”
Tuhin PaulPosted Oct 19, 2024, 4:31 AM
This typeof issue occurs when there's an issue with communication between the main thread and a background script, web worker, or iframe.
Can you wrap the SQL query in a
try-catchblock to catch any SQL-specific errors.