Javascript es2015:使用箭头功能转换为es2015
考虑到我有:Javascript es2015:使用箭头功能转换为es2015,javascript,ecmascript-6,Javascript,Ecmascript 6,考虑到我有: mongoose.connection.on('error', function (err) { if (err) { throw err; } }); 如何将其转换为ES2015语法 我试过: export class MongooseConnectionUtil extends MongooseUtil { constructor(...args) { super(...args); this.connection = mongoose.c
mongoose.connection.on('error', function (err) {
if (err) {
throw err;
}
});
如何将其转换为ES2015语法
我试过:
export class MongooseConnectionUtil extends MongooseUtil {
constructor(...args) {
super(...args);
this.connection = mongoose.connection;
}
on('error', err) => err {
if (err) {
throw err;
}
}
还尝试了()
mongoose.connection.on
接受两个参数,事件类型为字符串和函数。要转换为箭头函数语法,func(arg){…}
应该变成(arg)=>{…}
例如,要在创建MongooseConnectionUtil
实例时创建错误处理程序,请将
this.connection.on('error', (err) => {
if (err) {
throw err;
}
});
谢谢
此
用于什么?在构造函数中,此
引用正在创建的实例。因为您分配了this.connection
您的问题,所以我在示例中使用了它。应该能够使用:this.connection.on('error',err=>if(err)throw err)代码>。我不明白。班上怎么了?只需编写mongoose.connection.on('error',err=>{…})
。
this.connection.on('error', (err) => {
if (err) {
throw err;
}
});