question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't access Javascript expandoObject in Javascript expression

See original GitHub issue

Hope all is well! I am having issue accessing my expandoObject in the javascript expression.

I have a custom Handler for Javascript expression below:

public async Task Handle(EvaluatingJavaScriptExpression notification, CancellationToken cancellationToken)
{
    ...get the values somehow
    var values = new Database().getValues();
    var obj = ToExpandoObject(values);
    var context = notification.Engine;
    context.SetValue("entity", obj);
    var text = context.GetValue("entity"); //debug show that the expando value is stored!
}


public ExpandoObject ToExpandoObject(IEnumerable<CnsFormOps.Requests.Model.TransformedFormValues> values)
{
    var eo = new ExpandoObject();
    var eoColl = (ICollection<KeyValuePair<string, object>>)eo;

    foreach (var kvp in values)
    {
        eoColl.Add(new KeyValuePair<string, dynamic>(kvp.FieldName, kvp.TransformedValue));
    }
    return eo;
}

In my javascript template expression, I have

JSON.stringify(Object.keys(entity).map(key => entity[key])) + " " + 
JSON.stringify(entity)

the result of the string is

[] {}

I used a similar method with liquid template and it worked fine and it return the expando object just fine. I am using Elsa 1.2.2.29

I am not sure why it’s not working?? any pointers? as always really appreciate it!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
sfmskywalkercommented, Oct 27, 2020

Although I am not 100% sure, I do think it might be a limitation on Jint’s side. To work around that, we might consider adding some helper functions to the script execution scope that help dealing with this sort of types.

1reaction
craigfowlercommented, Oct 27, 2020

Yeah. I just ran into the exact same thing. I also went with @sfmskywalker 's workaround. I Newtonsoft’ed my object to a JSON string in C#-land and then stored that string in my variable. As it happened, I actually wanted to output that JSON string anyway in a Write HTTP Response activity, but if I wanted bits of its object graph I could have JSON.parse’d that string in JS.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Jint (.NET javascript parser) always returns ExpandoObject
This suggests to me that the underlying JavaScript method is actually returning an object rather than a string. You can use Newtonsoft.Json to ......
Read more >
Working with Dynamic Objects: Beyond the Basics ...
ExpandoObjects let you dynamically add members to your object at run time -- a great way to handle scenarios where you need a...
Read more >
ExpandoObject Class (System.Dynamic)
Represents an object whose members can be dynamically added and removed at run time.
Read more >
ExpandoObject as a source of the Grid with Autogenerated ...
I must note that I can't promise a pivot grid will handle expandos, at least in v1, and I very much doubt it...
Read more >
Making It Up as You Go Along with ExpandoObjects
You can give your users the ability to store any data they want, including stuff they make up at run time, by using...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found