Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

read/write values of another session #16

Closed
CaiooAndrade opened this issue Jul 22, 2018 · 3 comments
Closed

read/write values of another session #16

CaiooAndrade opened this issue Jul 22, 2018 · 3 comments

Comments

@CaiooAndrade
Copy link

Hi, I'm learning about this library. Could I be able to access values from other sessions?
I'm trying like this,

const session = (new LocalSession({ database: 'example_db.json' }));
bot.use(session.middleware());
console.log(session.getSession('170062604:170062604')); //<-- This like gives me the error below
TypeError: Cannot read property '_' of undefined

@TemaSM
Copy link
Contributor

TemaSM commented Jul 23, 2018

Hi @Iniro,
Thanks for your question!

Short answer:
You are trying to use sessions database synchronously, but it even didn't initialized yet.
Simply set storage type to be synchronous, so your code will looks like:

const session = new LocalSession({
  database: 'example_db.json',
  storage: LocalSession.storageFileSync
})
bot.use(session.middleware());
console.log(session.getSession('170062604:170062604'));

Detailed answer:
As default telegraf-session-local uses asynchronous storage file adapter for lowdb:

const
debug = require('debug')('telegraf:session-local'),
lowdb = require('lowdb'),
storageFileSync = require('lowdb/adapters/FileSync'),
storageFileAsync = require('lowdb/adapters/FileAsync'),
storageMemory = require('lowdb/adapters/Memory')

class LocalSession {
constructor (options = {}) {
this.options = Object.assign({
storage: LocalSession.storageFileAsync,

And there's a little bug - we cannot use sessions database before it actually initialized.
Meanwhile I'm working on telegraf-session-local, you can simply set storage type to be synchronous, like:

const session = new LocalSession({
  database: 'example_db.json',
  storage: LocalSession.storageFileSync
})

It's not so easy to keep code backward compatible, but I will try to introduce in v0.0.6 something new on how to deal with asynchronous adapters.

@CaiooAndrade
Copy link
Author

Thank you, your response was extremely helpful.

TemaSM added a commit that referenced this issue Aug 28, 2018
TemaSM added a commit that referenced this issue Aug 28, 2018
TemaSM added a commit that referenced this issue Aug 28, 2018
* Improved detection of empty session data

* ✅ Improved tests (better coverage)

* ✅ Added new tests (better coverage)
* Tests for `format.serialize` and `format.deserialize` functions of lowdb storage
* Test for `isPromise()` function
* Exporting `isPromise()` function now

* 🔧 Ignoring file `sessions.json`, which comes after running tests

* ⬆️ Upgrading dependencies

* Added .npmrc (disable package-lock.json)

* 📝 Improve documentation & JSDoc config  [ci skip]

* 💥 Default storage type: storageFileAsync -> storageFileSync, see #16

* 👌 Possible fix for #16 and #13

* 🔧 Move ESLint config from package.json to .eslintrc [ci skip]

* Fixing tests (TravisCI failing builds for Node 6)
@TemaSM
Copy link
Contributor

TemaSM commented Aug 28, 2018

@Iniro now you can access database (and sessions in it) in two ways:

  1. Synchronous storage (LocalSession.storageFileSync - default storage adapter since v0.0.6):
const session = new LocalSession()
bot.use(session.middleware())
console.log(session.getSession('170062604:170062604'))
  1. Asynchronous storage (LocalSession.storageFileAsync):
const session = new LocalSession({
  storage: LocalSession.storageFileAsync,
})
bot.use(session.middleware())
session.DB.then((db) => {
  console.log(db.get('sessions').getById('170062604:170062604').value())
})

@TemaSM TemaSM removed this from the v0.0.6 milestone Aug 28, 2018
@TemaSM TemaSM closed this as completed Sep 4, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants