I want to submit some data but if the textarea (name) is empty or null i want to use the value of the select to search, but for some reason my if/else doesn't work and always search using the name even if its empty and this gives an error beaacause it's empty
e.preventDefault();
if (name != null || name !== '') {
try {
setNameIntroduced(true);
let cardFound = completeArrayCards.filter((e) => e.name.toLowerCase() === name.toLowerCase());
setCardFoundIndividual(cardFound);
let arrayImages = Object.keys(cardFound[0].image)
.map(function (key) {
return cardFound[0].image[key];
});
if (cardFound[0].name !== null) {
setFound(true);
setName(cardFound[0].name);
setType(cardFound[0].type);
setImage(arrayImages[1]);
setAttribute(cardFound[0].attribute || cardFound[0].type);
setDescription(cardFound[0].desc);
setLevel(cardFound[0].level);
setRace(cardFound[0].race);
setAtk(cardFound[0].atk);
setDef(cardFound[0].def);
setCardsets(cardFound[0].card_sets);
setCardPrices(Object.keys(cardFound[0].card_prices)
.map(function (key) {
return cardFound[0].card_prices[key];
}));
if (type === 'Spell Card' || type === 'Trap Card') setIsMagicTrap(true)
}
} catch (err) {
console.log(err);
setFound(false);
}
} else if (name == null && type != null) {
try {
setMultipleResults(true);
setFound(true);
setNameIntroduced(false);
let cardsFound = completeArrayCards.filter((e) => e.type === type);
//console.log(cardsFound);
console.log('Multiple found: ' + cardsFound[0].name + '\n' + cardsFound[0].type + '\n' + cardsFound[0].image.image_url);
} catch (err) {
console.log(err);
setFound(false);
}
}
}```
Related
How do I check if the message content has multiple lines in Discord.js and how do I get it?
I tried using \n but no results.
Here's my code:
module.exports = {
name: 'eval',
aliases: ['evaluate'],
desciption: 'Owner only.',
category: 'Core',
usage: '{prefix}eval <javascript>',
async execute(client, message, args) {
if (message.author.id !== client.config.discord.ownerID) return;
try {
const code = args.join(' ');
let evaled = eval(code);
if (typeof evaled !== 'string') {
evaled = require('util').inspect(evaled);
}
if (typeof evaled === 'Discord.MessageEmbed' && evaled !== null && !Array.isArray(evaled)) {
return message.channel.send({embeds: [functions.clean(evaled)]});
} else {
return message.channel.send(functions.clean(evaled));
}
} catch (err) {
message.channel.send(`\`ERROR\` \`\`\`xl\n${functions.clean(err)}\n\`\`\``);
}
},
};
You did not show your attempt at checking with \n. It works just fine for me
message.content.includes("\n") //true if multi line
Here is a snippet that shows it works:
var str = `single line`;
console.log(str.includes("\n"))
str = `multi
line`;
console.log(str.includes("\n"))
I was making a discord bot with a 'ban' command (discord.js) and I keep getting this error.... [parsing error unexpected token case]
the script:
case 'ban': {
if (!isMod)
return;
let userID = args.includes('<#!') ? args.replace('<#!', '').replace('>', '')
: args.includes('<#') ? args.replace('<#', '').replace('<', '') : '';
if (userID == '') {
message.reply('Invalid user ID or mention.');
return;
}
message.guild.fetchMember(userID).then(member => {
member.kick("Banned by " + message.author.tag).then(m => {
message.channel.send('🔨 Banned <#' + userID + '>.');
}).catch(() => {
console.error;
message.reply('Could not ban the specified member.');
});
};
break;
});
User.ban({reason: banReason})
You missed a bracket.
case 'ban': {
if (!isMod)
return;
let userID = args.includes('<#!') ? args.replace('<#!', '').replace('>', '')
: args.includes('<#') ? args.replace('<#', '').replace('<', '') : '';
if (userID == '') {
message.reply('Invalid user ID or mention.');
return;
}
message.guild.fetchMember(userID).then(member => {
member.kick("Banned by " + message.author.tag).then(m => {
message.channel.send('🔨 Banned <#' + userID + '>.');
}).catch(() => {
console.error;
message.reply('Could not ban the specified member.');
});
});
break;
}
Check before break statement - that was the problem.
handleClick=()=>{
console.log("final course values"+ " "+this.state.selectCourseValue)
console.log("selected teacher"+ " "+this.state.selectedTeacher)
if(this.state.selectCourseValue ===null && this.state.selectedTeacher !== undefined)
{
console.log("please select")
message.warning('Add course')
return;
}
else{
console.log("Sucessfully updated")
}
}
If selectCourseValue is an array, then your condition should be probably this:
if (this.state.selectedTeacher && (this.state.selectCourseValue || []).length === 0) {
}
Checking that selectedTeacher is not empty and selectCourseValue is empty.
I'm trying to remove a value from an array in my database which I get from url.
I use the code below but I get nothing. It doesn't go to the for loop.
app.get('/remove/:medid/:tokenid', function(req, res) {
var medid = req.params.medid;
var token = req.params.tokenid;
var query = { tokenid: token, mediaid !== 'undefined' && mediaid.length > 0 }
user.find(query).exec(function(err, result) {
if (err) {
res.send('erooooooor')
}
if (!result) {
res.send('whooops, you dont have any media yet :)')
} else {
console.log('its here')
for (var i in result.mediaid) {
console.log(i)
if (i == medid) {
user.update({ tokenid: token }, { $pull: { mediaid: medid } }, function(err, result2) {
if (err) {
res.send('an error happened')
} else {
console.log('deleted')
//res.send('your tokenid is '+ token)
}
})
} else {
res.send('media id didnt match')
}
}
}
});
});
My database has 3 objects userid and tokenid which are strings and mediaid which is an array.
Also, I want to check if my mediaid array is null or not and exist is this code
mediaid !== 'undefined' && mediaid.length > 0
You have got your json syntax incorrect in following line
var query = {tokenid: token, mediaid !== 'undefined' && mediaid.length > 0}
You should probably just let your query be based on token id.
var query = {tokenid: token};
Also when you write for(var i in result.mediaid), for each iteration of loop, the variable 'i' is assigned the index of current element, not its value. So your if condition should be modified to check result.mediaid[i] == medid instead of i == medid. Modified loop should look something like:
for(var i in result.mediaid){
console.log(i);
if(result.mediaid[i] === medid){
user.update({tokenid: token}, {$pull: {mediaid: medid}},function(err, result2){
if (err){
res.send('an error happened');
}
else{
console.log('deleted');
//res.send('your tokenid is '+ token)
}
});
}
}
Please, you can replace to:
app.get('/remove/:mediaid/:tokenid', function(req, res){
var medid = req.params.mediaid;
var token = req.params.tokenid;
var query = {tokenid: token, mediaid !== 'undefined' && mediaid.length > 0}
UPDATED:
I recommended that print after console.log(medid, token);
I hoped help you - Jose Carlos Ramos
im using ag-Grid, but there is a issue when it filters my data, when i filter my data in the price column, it only works with numbers dot and not with commas.
Link: https://plnkr.co/edit/LDdrRbANSalvb4Iwh5mp?p=preview
Practical Example:
In the Price column select box equal and above insert "1.5" and than try inserting "1,5"
This is because this filter is a native one.
If you want to handle custom behaviour, define your own filter.
Documentation : https://www.ag-grid.com/angular-grid-filtering/index.php
A quick and dirty solution would be to monkey patch the NumberFilter like this :
NumberFilter.prototype.doesFilterPass = function (node) {
if (this.filterNumber === null) {
return true;
}
var value = this.valueGetter(node);
if (!value && value !== 0) {
return false;
}
var valueAsNumber;
if (typeof value === 'number') {
valueAsNumber = value;
}
else {
valueAsNumber = parseFloat(value.replace(',','.'));
}
switch (this.filterType) {
case EQUALS:
return valueAsNumber === this.filterNumber;
case LESS_THAN:
return valueAsNumber < this.filterNumber;
case GREATER_THAN:
return valueAsNumber > this.filterNumber;
default:
// should never happen
console.warn('invalid filter type ' + this.filterType);
return false;
}
};
Then changed line is here :
valueAsNumber = parseFloat(value.replace(',','.'));
So i found the problem, first i had to convert the value has a string than i needed to replace the dot by the comma, the problem with the answer above was first because of the data type and than the order of the properties of the replace function, but the problem now is that is not filtering correctly, if i search using equal option if gives me 2 values, instead a fixed one, code looks something like this:
Code:
NumberFilter.prototype.doesFilterPass = function (node) {
if (this.filterNumber === null) {
return true;
}
var value = this.valueGetter(node);
if (!value && value !== 0) {
return false;
}
var valueAsNumber;
if (typeof value === 'number') {
value = value.toString()
valueAsNumber = parseFloat(value.replace('.',','));
}
else {
valueAsNumber = parseFloat(value.replace('.',','));
}
switch (this.filterType) {
case EQUALS:
return valueAsNumber === this.filterNumber;
case LESS_THAN:
return valueAsNumber < this.filterNumber;
case GREATER_THAN:
return valueAsNumber > this.filterNumber;
default:
// should never happen
console.warn('invalid filter type ' + this.filterType);
return false;
}
};
Possible Solution:
NumberFilter.prototype.onFilterChanged = function () {
var filterText = utils_1.default.makeNull(this.eFilterTextField.value);
if (filterText && filterText.trim() === '') {
filterText = null;
}
var newFilter;
if (filterText !== null && filterText !== undefined) {
console.log(filterText);
// replace comma by dot
newFilter = parseFloat(filterText.replace(/,/g, '.'));
console.log(newFilter);
}
else {
newFilter = null;
}
if (this.filterNumber !== newFilter) {
this.filterNumber = newFilter;
this.filterChanged();
}
};