'How to store data in map with dynamic input in javascript

I'm buliding my first projetct with node.js and trying to store user's input with Map here's my code and 2 incoming input:

const command = new Map()

let commadParam = ['userAccount', 'john'] // <- first input then it should be store for later use

let account = 'userAccount-'+ commadParam[1]
consolo.log(account) // 'userAccount-john'

commadParam = ['charItem','BowJohn'] // <- simulate second input

let char = 'charItem-' + commadParam[1]
consolo.log(char) // 'charItem-BowJohn'

command.set(account, account.split('-')[1])
command.set(char, char.split('-')[1])

console.log(command)// Map{'userAccount-john' => 'john', 'charItem-BowJohn' => 'BowJohn'}

it works fine when i literly put 2 input like example, but when i ran my app and type first input then after like 1sec i type second input it overwrite the first map

how do i store first input with Map for later use (like 1-3sec later) or should i just use DB for storing(like redis)? Thanks!

Here is full code:

const replyMsg = async (reqBody, res) => {
    const reqBodyMsg = reqBody.events[0].type
    const command = new Map()

    if (reqBodyMsg === 'message') {
       
        let commandParam = reqBody.events[0].message.text.split(' ')

        let account = 'userAccount-' + commandParam[1]
        let char = 'charItem-' + commandParam[1]

        command.set(account, account.split('-')[1])
        command.set(char, char.split('-')[1])
  
        let accountName = command.get(account)
        let charName = command.get(char)

        //input user account for character list
        if (commandParam[0] === 'userAccount') {

            const dataString = await getChar(reqBody, res, accountName)

            console.log(dataString)
        }

        //input character name to get character equipment
        if (commandParam[0] === 'charItem') {
            // accountName = data.key[1]
            // console.log(commandParam[0])
            // const charName = commandParam[1]

            const dataString = await getItem(reqBody, res, accountName, charName)
            console.log(dataString)
        }
    }
}



Solution 1:[1]

Declare and initialize command outside the function. Then if commandParam[1] is different it should not overwrite the old values in the map.

I'm not sure why you need accountName and charName, they should be the same as commandParam[1].

const command = new Map()

const replyMsg = async(reqBody, res) => {
  const reqBodyMsg = reqBody.events[0].type

  if (reqBodyMsg === 'message') {

    let commandParam = reqBody.events[0].message.text.split(' ')

    let account = 'userAccount-' + commandParam[1]
    let char = 'charItem-' + commandParam[1]

    command.set(account, account.split('-')[1])
    command.set(char, char.split('-')[1])

    let accountName = command.get(account)
    let charName = command.get(char)

    //input user account for character list
    if (commandParam[0] === 'userAccount') {

      const dataString = await getChar(reqBody, res, accountName)

      console.log(dataString)
    }

    //input character name to get character equipment
    if (commandParam[0] === 'charItem') {
      // accountName = data.key[1]
      // console.log(commandParam[0])
      // const charName = commandParam[1]

      const dataString = await getItem(reqBody, res, accountName, charName)
      console.log(dataString)
    }
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barmar