0

I'm trying to send an email using a javascript code in a Java project. Database connection works fine, I already tested it. I got the error: javax.script.ScriptException: sun.org.mozilla.javascript.internal.EvaluatorException: missing ) after formal parameters (#1) in at line number 1

Nicolas
  • 45
  • 7

3 Answers3

2

The only information of relevance is not readily reported: the final JavaScript string executed. Make sure to look at relevant data when debugging. After inspecting the final string it will be apparent why it is incorrect and trivial to "fix".

Hint: it will look something like function sendMail(userblah, foo@bar.qux) { .., which is indeed invalid JavaScript.

The problem and solution should be self-evident from that - use fixed parameters (variable names) in the function declaration and supply arguments (values) via the invokeFunction call.


Solution:

// The following parameter names are JAVASCRIPT variable names.
String script = "function sendMail(username, email, body) { ..";

// And pass correct arguments (values), in order. The variables used
// here are JAVA variables, and align by-position with the JS parameters.
inv.invokeFunction("sendMail", username, email, "EMAIL SENT!!!!");

In addition, the getElementById (invalid ID) is wrong, the body parameter is never used, and encodeURIComponent should be used (instead of escape).

user2864740
  • 54,112
  • 10
  • 112
  • 187
  • @Nicolas After fixing the script parmeters, yes. Make sure that the parameters match up as well - where is "EMAIL SENT!!!" going? – user2864740 Apr 11 '14 at 22:16
  • 1
    String script = "`function sendMail(" + username + "," + email + ")" ..` is *still* wrong, per above -- **print/log the contents of the `script` variable before you eval it (or attach a debugger).** You *must* look at the relevant data to debug your problems! – user2864740 Apr 11 '14 at 22:33
  • I printed script and it's working: function sendMail(blabla,blabla@gmail.com) – Nicolas Apr 11 '14 at 22:34
  • That is **not** correct code and is indeed *invalid* JavaScript (try running the contents of `script` in a browser console). Re-read the Hint/Solution sections. Trivially, try running this in a browser: `function f(a@b) {}` - What error do you get? (It might not be the exact error message, but it's invalid for the same reason in either case: *parameters* are variables, are not values!) – user2864740 Apr 11 '14 at 22:34
  • You mean they should be *parameters names*? Or they should be Strings? – Nicolas Apr 11 '14 at 22:40
  • Parameters are *variables*, not strings/values. `function f("text") { /* this is wrong */ }`; vs `function f(text) { alert(text) }; f("text")`. In the latter case, `f` was *invoked* supplying the *value* ("text") as an *argument* which was bound to the *parameter* (declared as) `text` inside the function. The first syntax is of course, invalid just as `function f(blah@blah.com) { /* wrong still, blah@blah.com is an invalid identifier/syntax */ }` is invalid. Parameters are *variables* (which evaluate to values) but are *not* values. – user2864740 Apr 11 '14 at 22:41
  • I recorrected it but still not working... When I print *script* am I supposed to see the value of username and email in the email message? Cause I don't... – Nicolas Apr 11 '14 at 22:53
  • No. You will/should **not** see any values when printing `script`. You should see parameter names. And, **Parameters are *variables* (which evaluate to values) but are not *values***. The *values* are supplied to the function via *arguments* - as specified with `invokeFunction`. – user2864740 Apr 11 '14 at 22:55
  • Because it is invalid JavaScript. Can you *run* the function declaration, which is the contents of `script`, in a browser without errors? – user2864740 Apr 11 '14 at 22:59
  • Ha, thank you! I find the little "s that were bugging me. If by any luck you've used this type of implementation before, do you know how long does it take before it actually send an email? – Nicolas Apr 11 '14 at 23:12
  • @Nicolas Nope, I have no idea. I'm not even sure if it will work :) – user2864740 Apr 11 '14 at 23:13
0

Not sure if this is a typo or not:

    result = request.executeQuery("SELECT user.login, user.email "
                    + "FROM user " + );

It looks like you are missing the end of your statement.

dub stylee
  • 3,012
  • 5
  • 33
  • 57
  • While this is a problem, and the posted Java won't compile, it's not related to the reported error. Presumably it's an error generated during posting. – user2864740 Apr 11 '14 at 22:07
  • Yes it's a typo. Anyway, this part is compiling correctly, I get the right info in login and user. – Nicolas Apr 11 '14 at 22:12
0

Hmmmm, your function definition:

function sendMail("username","email") {...}

doesn't look like valid JavaScript to me, apart of that, you never call the function.

Pseudocode, how to do it:

function sendMail(username, email) {
    var link = "jadajada" + email; // .... etc
}
sendMail("+username+","+email+");
Udo Klimaschewski
  • 4,827
  • 1
  • 25
  • 40
  • My function is already sendMail(username,email) and not: function sendMail("username","email") ... *no?* – Nicolas Apr 11 '14 at 22:43