Getting the URL from a submitted image

时间:2017-08-05 11:35:30

标签: javascript node.js image discord discord.js

code with node.js and discord.js:

client.on('message', function(message){
  //  if (message.author.client) return;
    var Attachment = (message.attachments).array();
    console.log(Attachment); //outputs array
    console.log(Attachment.url); //undefined
    console.log(Attachment.MessageAttachment); //undefined
    console.log(Attachment.MessageAttachment['url']); //error
});

output of "console.log(Attachment);"

how do i get the string from

[MessageAttachment
  {...,
   url: '..png',
   ...}
]

1 个答案:

答案 0 :(得分:1)

From your screenshot of console, it looks like Attachment is an array, not an object. So, you would need to access the first element from that array and then the url property. Like this:

Attachment[0].url

Also, if there is a possibility of having multiple attachments, you could iterate over them using a for or forEach loop. Something like this:

Attachment.forEach(function(attachment) {
  console.log(attachment.url);
})