This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Trying to run JavsScript in Nashorn library

 
Bartender
Posts: 1757
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have code that works fine in JavaScript IDE like Webstorm, but when I try to port it to Java, using the Nashorn library (to run JS within a Java program), I get these errors: "Name: undefined, Age: undefined" (with possibly others to follow depending what's wrong here.)

Below is the full code. This coding approach below seems to work in general that is it works with "simpler" JavaScript scripts, but there are cases like this where I don't get the expected results.

Would appreciate any help or suggestions.

Thanks!

 
Sheriff
Posts: 3207
476
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem is that param is a string, not a JSON object.

I modified your code to construct an object from the string parameter, and then use that object in the subsequent code and it worked as expected:
 
Mike London
Bartender
Posts: 1757
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:The problem is that param is a string, not a JSON object.

I modified your code to construct an object from the string parameter, and then use that object in the subsequent code and it worked as expected:



Thanks very much. I mistakenly thought it was just the JS complexity since I had already used JSON.parse() in another example, but clearly forgot to use it here!

I noticed that in WebStorm IDE, also leading to the confusion, that the JS code worked fine w/o the JSON.parse().

Appreciate your excellent reply.

- mike
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic