我正在尝试在类构造函数中使用 ES6 解构,但出现未知标记错误。下面是一个例子:
// 导入/服务器/a-and-b.js
class A {
constructor(id) {
// make MongoDB call and store inside this variable
let {
firstName: this._FirstName // => Throws here
} = CollectionName.findOne({userId: id});
}
}
export class B extends A {
constructor(id) {
super(id);
}
get FirstName() {
return this._FirstName;
}
}
// 导入/服务器/test.js
import { B } from 'imports/server/a-and-b.js'
const b = new B('123')
const FirstName = b.FirstName;
同样的解构也适用于课堂之外:
// 另一个-test.js
// make MongoDB call and store inside this variable
let {
firstName: FirstName // works fine
} = CollectionName.findOne({userId: id});
我发现这可以这样做:
constructor(id) {
// make MongoDB call and store inside this variable
({ firstName: this._FirstName } = CollectionName.findOne({userId: id}));
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句