0

I am in a serious problem with my code. I am developing a bot for Telegram using Node.JS and ExpressJS. To make requests I use ngrok and to store information PostgreSQL. My problem is: when running the program with only one user connected and talking to the bot and processing operations, the code runs very well. But, when multiple users are connected at the same time, the program mixes the code for all users. Example:

If the first user activates a boolean variable for specific selection or if the bot is processing an SQL query for this user and showing the result, the second and third user are affected ... Why?

I think that maybe PostgreSQL doesn't allow multiple transactions with the database running in the local machine or maybe its a problem with the code.

How can I solve this?

danday74
  • 52,471
  • 49
  • 232
  • 283
  • [See How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) - you'll get a much better response if you can formulate a clear, concise, question including the relevant code. – Fraser Aug 17 '17 at 19:20

1 Answers1

0

It sounds like you are using a variable server side. Since you have not provided code I will provide an example illustrating what I think the problem is.

Say you have a variable called basket which stores all the items in the users shopping basket. You update that variable when user A puts something in their basket but you update the same variable when user B puts something in their basket.

Instead you need to create a users object like this:

let users = {
  "5": {
    userid: 5,
    basket: ['apple', 'banana']
  },
  "7": {
    userid: 7,
    basket: ['orange', 'apple']
  }
}

When you make a request to the backend you pass the ID and the new item in the request (POST body typically) and get the correct user as follows:

let user = users[param.userid]
user.basket.push(item)

Then each user will be updated individually.

halfer
  • 19,824
  • 17
  • 99
  • 186
danday74
  • 52,471
  • 49
  • 232
  • 283