Javascript 如何在Mongoose模型中使用符号?
我到处找,不仅如此,而且找不到答案。我找到了这个。我正在尝试使用,所以前面的链接没有帮助 我的班级看起来像这样Javascript 如何在Mongoose模型中使用符号?,javascript,mongodb,class,Javascript,Mongodb,Class,我到处找,不仅如此,而且找不到答案。我找到了这个。我正在尝试使用,所以前面的链接没有帮助 我的班级看起来像这样 import mongoose from 'mongoose'; const _user_key = Symbol('key'); const _user_name = Symbol('name'); const _user_email = Symbol('email'); class User extends mongoose.Schema { constructor(k
import mongoose from 'mongoose';
const _user_key = Symbol('key');
const _user_name = Symbol('name');
const _user_email = Symbol('email');
class User extends mongoose.Schema {
constructor(key,name,email) {
const user = super({
this[_user_key] = key;
this[_user_name] = name;
this[_user_email] = email;
})
}
get key () {return this[_user_key]};
get name (){return this[_user_name]};
get email () {return this[_user_email]};
};
export default mongoose.model('User', new User);
我从终点站出发
this[_user_key] = key;
^
SyntaxError: Unexpected token '['
at Loader.moduleStrategy (internal/modules/esm/translators.js:122:18)
at async link (internal/modules/esm/module_job.js:42:21)
如果我试一下Bergi的建议
class User extends mongoose.Schema {
constructor(key,name,email) {
const user = super(
this[_user_key] = key;
this[_user_name] = name;
this[_user_email] = email;
)
}
我出错了
this[_user_key] = key;
^^^
SyntaxError: missing ) after argument list
该错误看起来与符号无关,但与实际语法错误有关 你是说
super({
[\u user\u key]:key,
[\u user\u name]:名称,
[用户电子邮件]:电子邮件,
})
这只是一个语法错误。
super(…)
调用中的{
启动一个对象文本。它与符号无关,如果使用普通属性名,此语法也不起作用。@Bergi如何修复此问题?