I encountered a problem where the script fails to recognize the constructor in the class and incorrectly assumes that I am calling all the other functions as constructors.
class Articles {
constructor(dbName = ':memory:') {
return(async() => {
this.db = await sqlite.open(dbName)
const sql = //an Sql command goes here
await this.db.run(sql)
return this
})()
}
async all() {
const sql = 'SELECT users.user, articles.* FROM articles, users\
WHERE articles.userid = users.id;'
const articles = await this.db.all(sql)
for(const index in articles){
if(articles[index].photo === null) articles[index].photo = 'avatar.jpg'
const dateTime = new Date (articles[index].D&T)
const date = `${dateTime.getDate()}/${dateTime.getMonth()+1}/${dateTime.getFullYear()}`
articles[index].Date_Time = date
}
return articles
}
async add(data) {
console.log('ADD')
console.log(data)
return true
}
async close() {
await this.db.close()
}
}
export default Articles
Upon executing this section of the code:
router.post('/add', async ctx => {
const a = await new Articles(dbName)
try{
await new a.add(ctx.request.body)
return ctx.redirect('/?msg=new article added')
} catch(err) {
console.log(err)
await ctx.render('error', ctx.hbs)
} finally {
new a.close()
}
})
The error message I receive states:
It Keeps telling me that the function is not a constructor
If you have any insights or solutions, please help. Thank you.