If else displaying wrong answer when using fetch [closed] - javascript

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
So what I'm trying to do is create an if/else statement, where if your IP is from Japan the console log will show "You are in Japan", and if your IP is from any other country the console log will show "I guess you are in the USA". The only problem is, it doesn't work. Even when I use VPN to pretend I am from Japan, the console log still displays "I guess you are in the USA".
This is the code I currently have:
async function country() {
let response = await fetch("https://ipinfo.io/json?token=8a21f471f4e583")
let CountryResponse = await response.json()
console.log(CountryResponse)
if (CountryResponse == "JP") {
console.log('You are in Japan');
} else {
console.log('I guess you are in the USA');
}
}
country()
So basically I would just like for the console log to show "You are in Japan" if the IP is from Japan, anything else the console log can show "I guess you are in the USA".

if (CountryResponse.country === "JP") {
console.log('You are in Japan');
} else {
console.log('I guess you are in the USA');
}
The api you are calling does not return only the county code but a object witch contains this information

Related

EmailJS message can't be sent [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 23 hours ago.
Improve this question
I'm new to EmailJS and trying to use it on my portfolio in the contact form. I tried different solutions and still showing the same error, I think I'm stuck now
I created the account, activated it, activated MFA as well, and tried multiple codes from the doc of EmailJS and from tutorials and still showing me the same error on the console. I even changed the service_id, template_id, and public key.
The error: Uncaught The public key is required. Visit https://dashboard.emailjs.com/admin/account
code on HTML:
<script type="text/javascript"
src="https://cdn.jsdelivr.net/npm/#emailjs/browser#3/dist/email.min.js">
</script>
<script type="text/javascript">
(function(){
emailjs.init("My-Public-Key");
});
</script>
code on JS:
function SendMail(){ event.preventDefault();
var params ={
from_name : document.getElementById("fullname").value,
email_id : document.getElementById("email-id").value,
message : document.getElementById("message").value
}
const serviceID = "service_uzy3k87";
const templateID = "template_btcihm8";
emailjs.send(serviceID, templateID, params)
.then(res=>{
document.getElementById("fullname").value = "";
document.getElementById("email-id").value = "";
document.getElementById("message").value = "";
console.log(res);
alert("Your message sent successfully!!")
})
.catch(err=>console.log(err));
}

Discord.js - Bot is not leaving the channel if alone at the end of the queue [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm kind of new to discord.js coding and I wanted to make my music bot stay in the channel forever, but if there are no people listening, it will quit at the end of the playing music queue.
This is what I tried:
const queue = message.client.queue.get(message.guild.id);
if (!song) {
const endembed = new MessageEmbed().setColor("#F0EAD6")
.setAuthor(`Music Queue ended.`)
if (queue.channel.members.size != 1)
message.client.queue.delete(message.guild.id);
return queue.textChannel.send(endembed).catch(console.error);
if (!song) queue.channel.leave()
message.client.queue.delete(message.guild.id);
return queue.textChannel.send(endembed).catch(console.error);
}
Problem is that the bot will not leave (if alone) at the end of the music playing.
So the bot needs to stay forever in the channel if there is someone in the channel that is not another bot, and leave the channel if the queue ends and there is no one in the channel.
Try adding braces to your code like this:
if (!song) {
const endembed = new MessageEmbed().setColor("#F0EAD6").setAuthor(`Music Queue ended.`);
if (queue.channel.members.size != 1){
message.client.queue.delete(message.guild.id);
return queue.textChannel.send(endembed).catch(console.error);
}
queue.channel.leave();
message.client.queue.delete(message.guild.id);
return queue.textChannel.send(endembed).catch(console.error);
}

Javascript Discord Bot 'SyntaxError: missing ) after argument list' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I've been trying to create a discord bot that filters certain words and then sends a message afterwards. Here is my current code:
const Discord = require('discord.js');
const client = new Discord.Client();
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
//runs the message looker thingy
client.on('message', async message function {
//1 blacklisted words
let blacklisted = ['communism', 'fascism', 'socialism', 'conservatism', 'racism', 'sexism', 'nazism', 'marxism'] //words put , after the word
//2 looking for words
let foundInText = false;
for (var i in blacklisted) { // loops through the blacklisted list
if (message.content.toLowerCase().includes(blacklisted[i].toLowerCase())) foundInText = true;
}
// checks casesensitive words
//3 deletes and send message
if (foundInText) {
message.delete();
client.message.send('ISM SPOTTED. OFFICIAL ISM REPORT HAS BEEN FILED AND WILL BE OVERLOOKED BY THE HIGH ISM COUNCIL. ANYTHING YOU SAY CAN AND WILL BE USED AGAINST YOU IN A COURT OF LAW. YOUR FATE SHALL BE DETERMINED.');
}
});
However I keep experiencing an error saying:
client.on('message', async message function {
^^^^^
SyntaxError: missing ) after argument list
I have placed an end bracket everywhere in the line of code and it still doesn't work. Can someone please explain the error and tell me what to do to fix it?
A function definition needs to have parentheses for its arguments, even if there are none.
function(){}

Use xhr code error on switch/case (javascript) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I'm trying to display some modal depending on the xhr error code, but I don't know why my switch is failing:
error: function (xhr) {
var codigo_error = parseInt(xhr.status);
console.log("codigo_error: " + codigo_error);//this shows 404
switch (codigo_error){
case 404: body = "Error 404";
default: body = "Other";
}
modal(body);
}
I always get the default case but in console.log I can see the 404. i tried with case '404' but there's no difference.
And if I put if(codigo_error == 404) alert(codigo_error); I can see the alert with 404
I always use a break in my switch cases.
switch(codigo_error){
case 404:
body = "Error 404";
break;
default:
body = "Other";
break;
}
modal(body);
Try that and see if it works.

SyntaxError: Unexpected token [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
For some reason, Unexpected token keeps getting returned. Any thoughts on how to fix it?
var sleepCheck = function(numHours);
if (numHours>=8)
{
return "You're getting plenty of sleep! Maybe even too much!";
}
else
{
return "Get some more shut eye!";
}
Function in javascript have its body delimites by { and }:
var sleepCheck = function(numHours) { // <-- START HERE
if (numHours>=8)
{
return "You're getting plenty of sleep! Maybe even too much!";
}
else
{
return "Get some more shut eye!";
}
}; // <-- END HERE
Your syntax is just wrong. Try this instead:
var sleepCheck = function(numHours)
{
if (numHours>=8)
{
return "You're getting plenty of sleep! Maybe even too much!";
}
else
{
return "Get some more shut eye!";
}
};

Categories

Resources