为什么 Javascript 无法识别我的构造函数?
Why does Javascript not recognize my constructor?
我遇到一个问题,脚本无法识别 class 中的构造函数,并假设我正在调用所有其他函数作为构造函数。
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
当我运行这部分代码时:
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()
}
})
这是我得到的错误:
It Keeps telling me that the function is not a constructor
谁能帮忙
“添加”和“关闭”是您“文章”的方法class。调用这些方法时不要使用“new”关键字
router.post('/add', async ctx => {
const a = await new Articles(dbName)
try{
await a.add(ctx.request.body) // <- don't need "new" here
return ctx.redirect('/?msg=new article added')
} catch(err) {
console.log(err)
await ctx.render('error', ctx.hbs)
} finally {
a.close() // <- don't need "new" here
}
})
我遇到一个问题,脚本无法识别 class 中的构造函数,并假设我正在调用所有其他函数作为构造函数。
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
当我运行这部分代码时:
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()
}
})
这是我得到的错误:
It Keeps telling me that the function is not a constructor
谁能帮忙
“添加”和“关闭”是您“文章”的方法class。调用这些方法时不要使用“new”关键字
router.post('/add', async ctx => {
const a = await new Articles(dbName)
try{
await a.add(ctx.request.body) // <- don't need "new" here
return ctx.redirect('/?msg=new article added')
} catch(err) {
console.log(err)
await ctx.render('error', ctx.hbs)
} finally {
a.close() // <- don't need "new" here
}
})