Variable not saving properly

I have this function that runs when user logs on:

function InitialSetup(five, context, result)  {
    
    five.log('InitialSetup');
    // save userKey
    const userKey = five.currentUserKey();
    five.setVariable("UserKey", userKey);
    
    // save current date as formatted string
    const today = new Date(five.now());
    const formattedDate = today.toLocaleDateString('en-US', { 
    weekday: 'long', 
    year: 'numeric', 
    month: 'long', 
    day: 'numeric' 
    });
    five.setVariable("CurrentDate", formattedDate);

    // save fiscal year start date
    let fyStartMo = 7;
    let yr = today.getFullYear();
    let mo = today.getMonth() + 1; // month is 0-based, so add 1 before comparing to fy start month
    if (mo < fyStartMo) {
        yr -= 1;
    }
    five.setVariable("FyStartDate", new Date(yr, (fyStartMo - 1), 1));

    // set any needed variables
    five.setVariable("AddMode", false);
    const joinTableMap = {
        Reports: 'ReportMembers',
        StudyGroups: 'StudyGroupMembers',
        Portfolios: 'PortfolioMembers'
    };
    const setupForms = [
        'ReportsSetup',
        'StudyGroupSetup',
        'PortfolioSetup'
    ];
    five.setVariable('JoinTableMap', joinTableMap);
    five.setVariable('SetupForms', setupForms);

    return five.success(result);
}

When I run Five and log on, then open the inspector, I see this for JoinTableMap variable:

"JoinTableMap":string"{"Reports":"ReportMembers","StudyGroups":"StudyGroupMembers","Portfolios":"PortfolioMembers"}"

So this isn’t working. When I log the variable, I get this:

"JoinTableMap: \"{\\\"Reports\\\":\\\"ReportMembers\\\",\\\"StudyGroups\\\":\\\"StudyGroupMembers\\\",\\\"Portfolios\\\":\\\"PortfolioMembers\\\"}\""

This seems to confirm that the variable is actually a string instead of a JSON object.

How did I go wrong? Thanks…

Hi Ron,

I have tested your function, and it seems to be working fine.

After running your function and using ‘console.log’ and ‘five.log’, it returned the desired response, but in your example, the object is stringified, which is a JavaScript feature and not an issue.

Are you manipulating the property five.variable.JoinTableMap before logging it?
How are you logging the property five.variable.JoinTableMap?

Given your scenario, you need to parse your string into an Object.

Regards,
Elton S

Thanks for the reply Elton. I’m not manipulating the object at all. I created the object then assigned it to a variable. When I logged the variable later it came out as a string. Worse, the member names were included in the string but not the values. They showed /// instead of values. ChatGPT said setting the object into a variable was messing up the object during serialization process. They don’t always know how Five works, but suggested stringifying the object before storing it in a variable then parsing back to an object when I needed to use it. This does seem to work, and avoids serialization issues.

Does this sound reasonable to you?

Ron Mittelman

Hi Ron,

As mentioned, I use the same function, log it with either five.log or console.log, and assign the property to a new variable; the result is fine. That’s the reason I asked how you were logging it (the function used to log it or to retrieve it).
The image below shows the result of my tests.

If you cannot figure out in your code why the object is being stringified, you can use a similar approach to parse the string into an object. Below is an example of how you can achieve it using your scenario, but you may need to change it based on your needs.

Regards,
Elton S