Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception with latest JInt #7452

Closed
jptissot opened this issue Oct 30, 2020 · 6 comments · Fixed by #7485
Closed

Exception with latest JInt #7452

jptissot opened this issue Oct 30, 2020 · 6 comments · Fixed by #7485
Milestone

Comments

@jptissot
Copy link
Member

jptissot commented Oct 30, 2020

I am getting this exception with the latest dev branch.

An unhandled exception has occurred while executing the request.
System.InvalidOperationException: Cannot access child value on Newtonsoft.Json.Linq.JValue.
   at Jint.Runtime.Descriptors.Specialized.IndexDescriptor.get_CustomValue()
   at Jint.Native.Object.ObjectInstance.UnwrapJsValue(PropertyDescriptor desc, JsValue thisObject)
   at Jint.Native.Object.ObjectInstance.Get(JsValue property, JsValue receiver)
   at Jint.Runtime.TypeConverter.OrdinaryToPrimitive(ObjectInstance input, Types hint)
   at Jint.Runtime.TypeConverter.ToPrimitive(JsValue input, Types preferredType)
   at Jint.Runtime.Interpreter.Expressions.JintExpression.EqualUnlikely(JsValue x, JsValue y)
   at Jint.Runtime.Interpreter.Expressions.JintExpression.Equal(JsValue x, JsValue y)
   at Jint.Runtime.Interpreter.Expressions.JintBinaryExpression.EqualBinaryExpression.EvaluateInternal()
   at Jint.Runtime.Interpreter.Expressions.JintBinaryExpression.GetValue()
   at Jint.Runtime.Interpreter.Statements.JintIfStatement.ExecuteInternal()
   at Jint.Runtime.Interpreter.JintStatementList.Execute()
   at Jint.Engine.Execute(Script program)
   at OrchardCore.Scripting.JavaScript.JavaScriptEngine.Evaluate(IScriptingScope scope, String script) in C:\projects\orchardcore\src\OrchardCore\OrchardCore.Scripting.JavaScript\JavaScriptEngine.cs:line 47
   at OrchardCore.Scripting.DefaultScriptingManager.Evaluate(String directive, IFileProvider fileProvider, String basePath, IEnumerable`1 scopedMethodProviders) in C:\projects\orchardcore\src\OrchardCore\OrchardCore.Infrastructure\Scripting\DefaultScriptingManager.cs:line 45

This is the GlobalMethod I wrote that returns the JObject

            _formAsJsonObject = new GlobalMethod
            {
                Name = "requestFormAsJsonObject",
                Method = serviceProvider => (Func<JObject>)(() =>
                {
                    var httpContextAccessor = serviceProvider.GetRequiredService<IHttpContextAccessor>();
                    var sanitizer = serviceProvider.GetRequiredService<IHtmlSanitizerService>();

                    return new JObject(httpContextAccessor.HttpContext.Request.Form.Select(
                      field =>
                      {
                          var arr = field.Value.ToArray();
                          if (arr.Length == 1)
                          {
                              return new JProperty(field.Key, sanitizer.Sanitize(field.Value[0]));
                          }
                          return new JProperty(field.Key, JArray.FromObject(arr.Select(o => sanitizer.Sanitize(o))));
                      }
                    ).ToArray());
                }
                )
            };
        }

And the script that causes the exception:

var data = requestFormAsJsonObject();

if(data.name == "") {
 addError('name', 'test'); 
}
@sebastienros
Copy link
Member

Is that a JObject object that is returned from requestFormAsJsonObject() and you are trying to access the property name ?

@jptissot
Copy link
Member Author

jptissot commented Oct 30, 2020

Yes

the property name coming from a form, and mapped using this line:
return new JProperty(field.Key, sanitizer.Sanitize(field.Value[0]));

@jptissot
Copy link
Member Author

@sebastienros
Copy link
Member

Try data["name"]

@jptissot
Copy link
Member Author

Same error.

@sebastienros
Copy link
Member

Please update with https://www.nuget.org/packages/Jint/3.0.0-beta-1914

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants