如何在javascript中动态输入的map中存储数据



我正在用node.js构建我的第一个项目,并试图用Map存储用户的输入下面是我的代码和2个输入:

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

当我输入2个输入时效果很好例如,但当我运行我的应用程序并输入第一个输入大约1秒后我输入第二个输入它会覆盖第一个映射

我如何存储第一个输入与地图供以后使用(如1-3秒后)或我应该只是使用DB存储(如redis)?谢谢!

完整代码:

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

在函数外部声明并初始化command。然后,如果commandParam[1]是不同的,它不应该覆盖映射中的旧值。

我不知道你为什么需要accountNamecharName,它们应该和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)
}
}
}

相关内容

  • 没有找到相关文章

最新更新