Closed hyzx86 closed 2 years ago
The above tests use the latest code, but the tests in version 1.2.0 are normal
> log('Error',"mailListResult:"+mailListResult)
mailListResult:System.Collections.Generic.List`1[Newtonsoft.Json.Linq.JObject]
After ToArray
, it can be output normally
@sebastienros Is it a bug in Jint?
> log('Error',"mailListResult:"+mailListResult.ToArray().map(x=>x.Email))
["xxx@QQ.COM"]
After
ToArray
, it can be output normally @sebastienros Is it a bug in Jint?
@hyzx86 I don't think it is a bug in Jint. Please have a look at executeQuery implementation, it is returning IEnumerable<object>
which for Scripting is just a regular object, not an array, that is why you can't access map method. As you've noticed by converting object to an array you were able to use map method. You could also use spread operator to achieve the same.
here is original "executeQuery" method:
You can simply fix this, by returning an array:
(I've used Linq here, so please remember to add using System.Linq;
)
@lampersky Last year, I submitted a similar issue and created PR, which is the same as the code above, but the final solution is that orchardcore updated jint. After a period of time, there was no such problem, but the latest version reappeared. I don't know why
@lampersky
So your solution is to re inject an executeQuery
implementation?
Just like this?
services.AddSingleton<IGlobalMethodProvider, MyQueryGlobalMethodProvider>();
I tested this method, and it really works,
However, there will be two such methods in the system. I don't know whether it will cause unexpected problems
I don't know whether it will cause unexpected problems
I would say you will also need to replace DefaultScriptingManager with your own implementation, where you can choose which method should be used. Orchard Core will accept many GlobalMethods with the same name, but you can't be 100% sure which one will be used inside JavaScriptEngine (IScriptingEngine).
Here you can see, my own module where I've duplicated one of exisiting GlobalMethod:
and here you can see how those methods are registered within Jint.Engine, as you can see methods with the same name overwrite existing ones:
@hyzx86 I hope that helps :)
@lampersky Thanks for sharing I will to refer to your code to solve this problem,
@sebastienros At the same time, I submitted my replication test on Jint
Script:
log output