我正在尝试匹配"83eec6e44ea04dee9103e845ad51c4f0"从这个json,但我得到一个错误(如下所示)。顺便说一句,我从httpget请求中得到了json。从阅读错误自己,它看起来像它有一些与httpget,但我得到的数据从请求很好。
username = message.content;
var request = require('request');
request(`https://api.mojang.com/users/profiles/minecraft/${username}`, function(error, response, body) {
if (!error && response.statusCode == 200) {
// body is this: body = {"name":"uhWillem","id":"83eec6e44ea04dee9103e845ad51c4f0"};
console.log(body)
message.channel.send(body);
regex = "(?<=id":")(.*)(?=")";
match = body.match(regex);
message.channel.send(match)
}
})
错误:
C:UserswilleOneDriveBureaubladdiscordBotnode_modulesdiscord.jssrcrestRequestHandler.js:298
throw new DiscordAPIError(data, res.status, request);
^
DiscordAPIError: Cannot send an empty message
at RequestHandler.execute (C:UserswilleOneDriveBureaubladdiscordBotnode_modulesdiscord.jssrcrestRequestHandler.js:298:13)
at processTicksAndRejections (node:internal/process/task_queues:96:5)
at async RequestHandler.push (C:UserswilleOneDriveBureaubladdiscordBotnode_modulesdiscord.jssrcrestRequestHandler.js:50:14)
at async TextChannel.send (C:UserswilleOneDriveBureaubladdiscordBotnode_modulesdiscord.jssrcstructuresinterfacesTextBasedChannel.js:172:15) {
method: 'post',
path: '/channels/883457777035534417/messages',
code: 50006,
httpStatus: 400,
requestData: {
json: {
content: undefined,
tts: false,
nonce: undefined,
embeds: undefined,
components: undefined,
username: undefined,
avatar_url: undefined,
allowed_mentions: undefined,
flags: undefined,
message_reference: undefined,
attachments: undefined,
sticker_ids: undefined
},
files: []
}
}
我建议您解析JSON。对于更复杂的JSON,它更简单,可能更快。
if (!error && response.statusCode == 200) {
let { id } = JSON.parse(body);
message.channel.send(id);
}
如果您需要使用regexp,您需要知道.match(regex)
返回一个数组,因此您应该只选择第一个元素:
matches = body.match(regex); // Don't name it match... It's an array :)
message.channel.send(matches[0]);
但是可以使用性能更高的代码!您可以在id键(我们知道名称key == username)和.substring()
之前对文本进行字符串化:
if (!error && response.statusCode == 200) {
let pre = `{"name":"${username}","id":"`;
// id is 32 characters long (16 bytes)
let id = body.substring(pre.length, pre.length+32);
message.channel.send(id);
}
如果你想在request
回调之外使用id,你需要将所有内容包装在一个承诺和一个专用函数中:
const request = require('request');
function get_username_id(username) {
return new Promise((resolve, reject) => request(
`https://api.mojang.com/users/profiles/minecraft/${username}`,
function(error, response, body) {
if (error || response.statusCode != 200) reject(error || response.statusCode)
// body is {"name":"uhWillem","id":"83eec6e44ea04dee9103e845ad51c4f0"};
resolve(JSON.response(body));
});
}
let username = message.content;
get_username_id(username)
.then(({name, id}) => {
console.log(`The id of ${name} is ${id}`); // here we should see the name == username and the id
// here we work with the data, eg we send it
message.channel.send(id);
// you can return a promise that resolves to something else and chain it with an other .then()
return Promise.resolve('This will be recived after the first promise');
})
.then(text => {
console.log(text);
// id and name are not accessible anymore, but you can pass them on...
})
.catch(err => console.error(err)); // here we print the error or status code different from 200
这是相当长的和更复杂的,但允许在未来的开发更灵活。
我建议你四处看看,找到更多关于承诺的信息。
body
包含JSON,如果您想提取id,只需使用
const match = JSON.parse(body).id