Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

207
Vistas
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)
        }
    }
}

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda