0

Im writing a skill for Alexa in JS and I have a problem with creating user objects so more then 1 user could use my code at the same time. I tried doing it in this way at the beginning of my code:

class MyUser {
  constructor() {
    this.name= "";
    this.number = 0;
    this.question= "";
  }
}
var myUser = new MyUser();

but it makes no sense :( How can I create a user depending on session? Every Alexa user gets session userId and I get this variable everytime user starts my skill:

event.session.user.userId

How can I dynamically use this feature? So if every user starts my script Im sure there is a new user object created and 2 or more people can use my skill parallel

Does it make sense something like this?

class MyUser {
  constructor() {
    this.name= "";
    this.number = 0;
    this.question= "";
  }
}
event.session.user.userId = new MyUser();

So Im able to get into value name? Like this? console.log("event.session.user.userId.name ");

I would like to create a session user object in that place:

var newSessionHandlers = {
  "NewSession": function () {

    console.log(this.event.session.user.userId);

      this.emit("Welcome");
  }
Anna K
  • 1,666
  • 4
  • 23
  • 47
  • https://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript – ddeadlink Jul 26 '17 at 11:08
  • Looks like you are already getting an `this.event.session.user` object. Don't create it yourself. – Bergi Jul 26 '17 at 11:35
  • but I would like that I can connect a value to this object. For example Im asking my user for a name. Users say my name is Anna, and after that I can call to this name this.event.session.user.name and I will get Anna name – Anna K Jul 26 '17 at 11:38

1 Answers1

1

What is event.session in your case? Generally, there are lot of solutions for session persistence. If this application is web-server, you can use complete library session providers, implemented like middleware. Objects will live within host process, like this in Express:

app.set('trust proxy', 1) // trust first proxy
app.use(session({
  secret: 'keyboard cat',
  resave: false,
  saveUninitialized: true,
  cookie: { secure: true }
}))

If you need some persistence, use store option, like store: new RedisStore, in previous config.

Vladislav Ihost
  • 2,127
  • 11
  • 26