microsoft / ClearScript

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
https://microsoft.github.io/ClearScript/
MIT License
1.79k stars 149 forks source link

microsoft.clearscript.v8 in use promise but return this {[undefined]} or {Microsoft.ClearScript.V8.V8ScriptItem.V8ScriptObject} #550

Closed NamanGajjar26 closed 1 year ago

NamanGajjar26 commented 1 year ago

In this code I am using microsoft.clearscript.v8 library in c# (asp .net core mvc)

how to solve this error '((Microsoft.ClearScript.ScriptItem)promiseResult).UnderlyingSystemType' threw an exception of type 'System.NotImplementedException' in server side javascript run on asp .net core mvc

this below code is Home controller in call js file()

  `public IActionResult Index()
     {

    using (var engine = new V8ScriptEngine())
    {
        string scriptPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "js", "NewScript.js");
        string scriptContent = System.IO.File.ReadAllText(scriptPath);
        engine.Execute(scriptContent);

        try
        {

            dynamic promiseResult = engine.Script.newfunction();                   

           ViewBag.Result = promiseResult;

        }
        catch (Exception ex)
        {
            // Handle exceptions if the promise is rejected
            string result = $"Error: {ex.Message}";
            ViewBag.Result = result;
        }

    }
  }`

this below code in JavaScript NewScript.js

  `function newfunction() {    
  const promise1 = Promise.resolve("This is promised 1");

promise1.then((value1) => {        
   return value1;        
  });

}`

this newfunction return this {[undefined]} or {Microsoft.ClearScript.V8.V8ScriptItem.V8ScriptObject}

get this error UnderlyingSystemType = '((Microsoft.ClearScript.ScriptItem)promiseResult).UnderlyingSystemType' threw an exception of type 'System.NotImplementedException'

ClearScriptLib commented 1 year ago

Hi @NamanGajjar26,

this newfunction return this {[undefined]} or {Microsoft.ClearScript.V8.V8ScriptItem.V8ScriptObject}

As shown above, newfunction returns no value; that is, it always returns undefined. Also, your Index method doesn't compile, because it doesn't return anything. Please clarify or provide a more complete sample.

get this error UnderlyingSystemType = '((Microsoft.ClearScript.ScriptItem)promiseResult).UnderlyingSystemType' threw an exception of type 'System.NotImplementedException'

We've tested your code in a newly created ASP.NET Core project, and we didn't see that exception. It's true that script objects in ClearScript throw NotImplementedException for the UnderlyingSystemType property, but we aren't clear about why that property is being invoked. Can you share a stack trace?

Thanks!

NamanGajjar26 commented 1 year ago

solved this error

NamanGajjar26 commented 1 year ago

thank you for answering my question