question

Ozan Yilmaz avatar image
Ozan Yilmaz asked

Returning variable as string from cloud script?

Hello everyone,

I am trying to return a variable from the cloudscript as string.

Here, I simplified the code down below. The "result" variable changes depending on some conditions then I return the variable as json format. When I print the function result, it shows me {"result":100} instead of {"success":100} or {"fail":100}.

    var result;
    
    if(/* condition */) {
        //... code
        result = "success";
    }
    else {
        //... code
        result = "fail";
    }
    
    return { result: 100 };
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
Citrus Yan avatar image
Citrus Yan answered

You can use bracket notation directly in object declaration:

{[key]: value }

Where “key” can be any sort of expression, in your case, a variable returning a value.

Therefore, in your code, you should change line 12 to the following statement to return the desired result:

return { [result]: 100 };
1 comment
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Ozan Yilmaz avatar image Ozan Yilmaz commented ·

It works, thank you.

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.