KevHau
KevHau

Reputation: 13

Getting the URL from a submitted image

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',
   ...}
]

Upvotes: 1

Views: 17564

Answers (1)

Nisarg Shah
Nisarg Shah

Reputation: 14541

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

Upvotes: 1

Related Questions