简体   繁体   中英

How to use Symbols with Mongoose model?

I looked everywhere not only SO,but couldn't find the answer. I found this mongoose-model-ES6 . I am trying to use Symbol data type. so the previous link does not help.

My class looks like this

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);

I run from terminal

            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)

If I try what Bergi suggested

class User extends mongoose.Schema {
    constructor(key,name,email) {
        const user = super(
            this[_user_key] = key;
            this[_user_name] = name;
            this[_user_email] = email;              
        )        
    }

I got error

            this[_user_key] = key;
                              ^^^

SyntaxError: missing ) after argument list

The error does not look related to symbols but to an actual syntax error

Did you mean

super({
  [_user_key]   : key,
  [_user_name]  : name,
  [_user_email] : email,             
})        

?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM