Cannot make D1 requests twice?

I am facing a strange issue where I am unable to make D1 queries multiple times. Here is the code I am using
let sqlQuery = `SELECT sura, aya, text FROM en_pickthall WHERE 1=1 `;
keywords.forEach((keyword, index) => {
sqlQuery += `AND (`;
sqlQuery += `text LIKE '% ${keyword} %' `;
sqlQuery += `OR text LIKE '% ${keyword}.' `;
sqlQuery += `OR text LIKE '% ${keyword},' `;
sqlQuery += `OR text LIKE '% ${keyword}?' `;
sqlQuery += `OR text LIKE '% ${keyword}!' `;
sqlQuery += `OR text LIKE '% ${keyword}%' `;
sqlQuery += `) `;
});
sqlQuery += `LIMIT ${limit};`;

// Execute the query
let { results } = await env.DB.prepare(sqlQuery).all();
console.log(results)
let { contextVerses } = await env.DB.prepare(sqlQuery).all();
console.log(contextVerses);
let sqlQuery = `SELECT sura, aya, text FROM en_pickthall WHERE 1=1 `;
keywords.forEach((keyword, index) => {
sqlQuery += `AND (`;
sqlQuery += `text LIKE '% ${keyword} %' `;
sqlQuery += `OR text LIKE '% ${keyword}.' `;
sqlQuery += `OR text LIKE '% ${keyword},' `;
sqlQuery += `OR text LIKE '% ${keyword}?' `;
sqlQuery += `OR text LIKE '% ${keyword}!' `;
sqlQuery += `OR text LIKE '% ${keyword}%' `;
sqlQuery += `) `;
});
sqlQuery += `LIMIT ${limit};`;

// Execute the query
let { results } = await env.DB.prepare(sqlQuery).all();
console.log(results)
let { contextVerses } = await env.DB.prepare(sqlQuery).all();
console.log(contextVerses);
If I run this code the console shows the first log is normal, but the second console.log returns undefined. What is going on here and how do I fix it?
4 Replies
kian
kian5mo ago
Because contextVerses doesn’t exist on a D1Result I assume? You’re destructuring.
Codename_A
Codename_AOP5mo ago
Im sorry, what does this mean? And how do I fix it? Why does it work on one var and not the other when they are exactly the sam except for names? okay declaring the var with {} and then accessing .results yields the correct value, but im still really confused on why this is happening
kian
kian5mo ago
Because you’re destructuring, you’re accessing a property - and “contextVerses” isn’t a property
kian
kian5mo ago
MDN Web Docs
Destructuring assignment - JavaScript | MDN
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
Want results from more Discord servers?
Add your server