in dao.js exports.pageContent = (pageid) => { return new Promise((resolve, reject) => {…
- User
in dao.js exports.pageContent = (pageid) => { return new Promise((resolve, reject) => { const sql =
SELECT title AS title, authorId AS authorId, publishDate AS publishDate blocks AS blocks FROM pages WHERE id=?; db.get(sql, [pageid], (err, row) => { //db.get returns only the first element matching the query if (err) { reject(err); return; } if (row == undefined) { resolve({ error404: "Page not found." }); //Page not found } else { const objBlocks = JSON.parse(row.blocks); const info = { title: row.title, authorId: row.authorId, publishDate: dayjs(row.publishDate), blocks: objBlocks}; resolve(info); } }); }); };the table pages has a column called blocks. the content of the field linked to id=10 is [{"blockType":"Header","content":"Brasilh3333"},{"blockType":"Image","content":"http://localhost:3000/images/brasil.jpg"}] is this function right?
- Assistant
The function looks mostly correct, but there is a minor syntax error in the SQL query. You need to add a comma between
publishDate AS publishDateandblocks AS blocks. Here's the corrected function:exports.pageContent = (pageid) => { return new Promise((resolve, reject) => { const sql = ` SELECT title AS title, authorId AS authorId, publishDate AS publishDate, blocks AS blocks FROM pages WHERE id=?`; db.get(sql, [pageid], (err, row) => { //db.get returns only the first element matching the query if (err) { reject(err); return; } if (row == undefined) { resolve({ error404: "Page not found." }); //Page not found } else { const objBlocks = JSON.parse(row.blocks); const info = { title: row.title, authorId: row.authorId, publishDate: dayjs(row.publishDate), blocks: objBlocks}; resolve(info); } }); }); };With this correction, the function should work as expected, assuming the rest of your implementation is correct. The function will return an object containing the title, authorId, publishDate, and the blocks, parsed from JSON, for the page with the specified .
- Assistant
If you have any more questions or need further assistance, please feel free to ask.