Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

javascript - 将请求答案存储在变量中(Store request answer in a variable)

new to JavaSript i'm currently working on a discord bot using an API.

(我是JavaSript的新手,我目前正在使用API??开发不和谐的机器人。)

And I would like to know how I could store the request answers in some variables so I can use them later to write on a .json file.

(而且我想知道如何将请求答案存储在一些变量中,以便以后可以使用它们在.json文件中进行写操作。)

API used : https://wiki.guildwars2.com/wiki/API:2/account (I can show the answer with a console.log without problems but I cant store them :/ )

(使用的API: https ://wiki.guildwars2.com/wiki/API:2/帐户(我可以使用console.log显示答案,但不会出现任何问题,但我不能存储它们:/))

bot.on('message', message => {
  if (message.content.startsWith(config.prefix+"add")){
    const fs = require('fs') //importing file save
    var fPath = './data.json'
    var fRead = fs.readFileSync(fPath);
    var fFile = JSON.parse(fRead); //ready for use
    var userId = message.author.id //user id here
    var n; var g;
    if (!fFile[userId]) { //this checks if data for the user has already been created
        cut=message.content.replace("?add ", "");
        api.authenticate(cut).account().blob().then(request=> n=(request.account.name))
        api.authenticate(cut).account().blob().then(request=> g=(request.account.guilds)); 

        fFile[userId] = {key: cut, uidgw2:n, guild: g, role:""} //if not, create it
        fs.writeFileSync(fPath, JSON.stringify(fFile, null, 2));
    } else {
        message.reply("Déjà dans la base.");
        console.log(message.author.id+" déjà dans data.json");
}
  }
})

My problem is that n & g are apparently undefined and so i get that in my .json file :

(我的问题是n&g显然是未定义的,所以我在.json文件中得到了它:)

{
  "231452514608360960": {
    "key": "B1257308-125X-8040-A55B-0AD1CF03480DF08F4AC2-9326-44DC-83A6-75A950C5ADFA",
    "role": ""
  }
}

Even if I declare them outside bot.on or inside, still undefined and I dont have much knowledge in js & api requests...

(即使我在bot.on或内部声明它们,仍然是未定义的,并且我对js和api请求没有太多了解...)

  ask by Trijoikolip translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT: based on the further explanation added to the original question:

(编辑:根据添加到原始问题的进一步解释:)

You have to remember that the promises from the API calls are resolved after the initial pass.

(您必须记住,API调用中的承诺在初次通过后就已解决。)

yet you try and use the values already in the first pass.

(但您尝试使用第一遍中已经存在的值。)

Something like this should work:

(这样的事情应该起作用:)


Promise.all([api.authenticate(cut).account().blob(), 
             api.authenticate(cut).account().blob()])
           .then((requests) => {
                n = requests[0].account.name;
                g = requests[1].account.guilds;

                fFile[userId] = {key: cut, uidgw2:n, guild: g, role:""} //if not, create it
               fs.writeFileSync(fPath, JSON.stringify(fFile, null, 2));
            });
});


You havent shown what is failing here but if I were to guess Id say your issue has to do with the fact youre declaring 'n' and 'g' both outside the message callback and inside.

(您还没有显示出这里失败的原因,但是如果我想Id说您的问题与您在消息回调内部和内部都声明'n'和'g'有关。)

you have

(你有)

 var n; var g;

inside the callback but also at the first two lines.

(在回调中,也可以在前两行中。)

if you remove the declaration inside the callback you will be able to access the values outside.

(如果您在回调中删除声明,则可以访问外部的值。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...