Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

206
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda