Shorthand code
Answered
Japanese Bobtail posted this in #help-forum
Japanese BobtailOP
I'am building a meta-description with this code:
Is there a better way to shorthand this code (without using a custom function)? Maybe i has to use types?
description: (product?.excerpt && typeof product?.excerpt === 'string' && product?.excerpt.length) || product?.text,Is there a better way to shorthand this code (without using a custom function)? Maybe i has to use types?
20 Replies
@Japanese Bobtail I'am building a meta-description with this code:
`description: (product?.excerpt && typeof product?.excerpt === 'string' && product?.excerpt.length) || product?.text,`
Is there a better way to shorthand this code (without using a custom function)? Maybe i has to use types?
product?.excerpt?.length || product?.textwill this work?
@James4u tsx
product?.excerpt?.length || product?.text
Japanese BobtailOP
Yes man thanks.
@Japanese Bobtail Yes man thanks.
wait, updated check again
@James4u wait, updated check again
Japanese BobtailOP
This code looks way better, but it writes the length of the string (in my base 17) instead of the text.
what did you originally want to return?
@James4u what did you originally want to return?
Japanese BobtailOP
The excerpt (its a field to set the meta-desciption manually) sometimes is empty. So in this case i want to use the normal description text for the meta-description.
Your second code is returning the length of the string (17) instead of the text itself. Only the first code works fine.
Your second code is returning the length of the string (17) instead of the text itself. Only the first code works fine.
do we need to check type of
excerpt?if it's always a string you can do
product?.excerpt || product?.textif you it's not always a string
(typeof product?.excerpt === 'string' && product?.excerpt.length) || product?.text@James4u what do you think?
Japanese BobtailOP
We dont need to check the type because its a string, every time. But it can be empty.
okay then
product?.excerpt || product?.textAnswer
@James4u it should be enought then
Japanese BobtailOP
You will get the answer later... have to check something.
@James4u okay no worries
Japanese BobtailOP
Finally it works with
I fixed the bug in my api. It saved (and returned) whitespaces and carriage return. So the frontend value was never "empty" and clean. Thanks man. You solved it.
product?.excerpt || product?.text I fixed the bug in my api. It saved (and returned) whitespaces and carriage return. So the frontend value was never "empty" and clean. Thanks man. You solved it.
just mark solution to close this thread!