can't pass variable as url parameter [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 2 years ago.
Improve this question
I have some trouble with the code below:
function SidebarChat() {
const [seed, setseed] = useState("")
useEffect(() => {
setseed(Math.floor(Math.random() * 5000));
}, []);
return (
<div className="sidebarChat">
<Avatar src={'https://avatars.dicebear.com/api/human/${seed}.svg'} />
</div>
)
}
I can't pass ${seed} in the URL.
When I pass, its showing an error in the URL line and the '${seed}' is considered as a hardcoded string.

You're using quotes instead of backquotes when expressing the src, so the ${seed} won't be interpreted as it should be
To fix the issue:
<Avatar src={`https://avatars.dicebear.com/api/human/${seed}.svg`} />

I think yo need to remove string const [seed, setseed] = useState("") to const [seed, setseed] = useState()

To add variable in string you have to put it in template literals as below:
`https://avatars.dicebear.com/api/human/${seed}.svg`
OR
you can also use variable in single quote as shown below:
'https://avatars.dicebear.com/api/human/' + seed + '.svg'
To learn more about template literals

Related

Use .find() on a JSON file to get a a string with a specific keyterm [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 28 days ago.
Improve this question
I'm trying to use array.find() to get the first JSON string that contains amogus. I've tried to read the JSON file using fs then using JSON.parse() to make it readable. However, I can't seem to get it to do what I want it to do.
JSON file:
{stuff: ["hosds29083", "amogus1208", "amogus1213"]
desired output: amogus1208
Thanks.
Here is a possible solution:
const json = JSON.parse(data);
const first = json.stuff.find(obj => obj.includes("your string"))
const foundString = yourParsedJsonObject.stuff.find(string => string.includes("amogus"))
const json = `{
"stuff": ["hosds29083", "amogus1208", "amogus1213"]
}`
const data = JSON.parse(json)
const findData = data.stuff.find(s=>s.includes("amogus"))
console.log(findData)

JavaScript function printing itself rather than returning value [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 trying to return a Boolean value based on truthy of a given condition from an arrow function. The code is look like following
checkIsBasicInformationCompleted() {
const info = this.basicInformation;
const valid = () => {return !!(info.firstName && info.lastName && info.email && info.phone);};
console.log(valid);
},
But here instead of returning true/false, this function is printing itself. Can anybody explain the thing running here ? And how can I get a true/false value here ?
Fiddle sample: https://jsfiddle.net/tebz4Lc3/
You are printing a reference to the function here, use console.log( valid() ) to actually execute the function and print the return value.
console.log(valid());
This is how the logging should be.

Why does app.use(app.static(...)); does not work? [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 am new to programming in general. Currently learning about Javascript, node.js, and express...
const app = express();
const path = require('path');
Why does app.use(express.static(path.join(...))); works while app.use(app.static(path.join(...))); does not work?
Aren't they the same thing?
Aren't they the same thing?
No.
The return value of a function is not the same thing as the function.
What does it mean when I assign a function to a variable? const app = express()
You are not assigning a function to a variable. You are assigning the return value of a function to a variable.
function add(a, b) { return a + b };
const assignAFunction = add;
const assignTheReturnValue = add(1,1);
console.log({ assignAFunction, assignTheReturnValue });

setState value in async button handler function not getting empty in render [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 am trying to set value in asyn buttonHander function. it's working fine in onSortedViewHandler function but when i try to console.log array value in render arrayis empty, i don't know why but it's not rendering
state = { inputText: "", sortedArrayList: [] };
onSortedViewHandler = async () => {
const data = await this.props.sortedContract.methods
.sortNumberArrayList()
.call()
.then((array) => {});
this.setState({ sortNumberArrayList: data }); // array output is correct here
console.log(data);
console.log(this.state.sortNumberArrayList); // array output is correct here
};
it's working fine in this funtion,
but not array show empty in render
render() {
console.log(this.state.sortedArrayList); //but empty here
}
``
render() {
console.log(this.state.sortNumberArrayList);
}
This should render you the correct one. You had different naming state.

How can i show data present inside the nested elements using map function [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
Please tell me how can i access the nested objects in arrays
I have pasted the code below and I want to access the nested objects using
only through map function.
Data=[
{name:'Amir'},
{name:'jamile'},
{name:'hali'}
]
const result=Data.name.map( (name) => name);
console.log(result);
You can do it either by accessing the property using . operator or by destructing.
let Data=[
{name:'Amir'},
{name:'jamile'},
{name:'hali'}
]
const result=Data.map( ({name}) => name);
console.log(result);
const result2=Data.map( (name) => name.name );
console.log(result2);

Categories

Resources