Error - Expected 2 Arguments but got 0

I am having the following error with this code:

insertData(fname, lname) {
    const body = JSON.stringify({firstName: fname, lastName: lname});
}

Open in new window


Error: Expected 2 Arguments but got 0

How can I fix this?
LVL 1
error2013Asked:
Who is Participating?
 
Ryan ChongCommented:
check JSON.stringify syntax

JSON.stringify()
https://www.w3schools.com/js/js_json_stringify.asp

hence try:

function insertData(fname, lname) {
 var body = JSON.stringify({"firstName": fname, "lastName": lname});
 return body;
}
0
 
error2013Author Commented:
Still getting the same error:

  insertData(fname, lname) {
    const body = JSON.stringify({'firstName': fname, 'lastName': lname});
    return this.http.post('http://myurl/insert.php', body, httpOptions);
  }

Open in new window

0
 
Ryan ChongCommented:
>>Still getting the same error:

you're writing something different.

most likely the return part is error
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Ryan ChongCommented:
use browser's Inspect tool to verify the exact errors
0
 
error2013Author Commented:
The errors are pointing to this line:

const body = JSON.stringify({'firstName': fname, 'lastName': lname});

Open in new window


And only appeared when I added fname and lname to the function
0
 
SteHCommented:
Where do you exactly get the error? in the JSON line, or when calling the function?
In the latter case look where this function is called and if always with two parameters
0
 
error2013Author Commented:
ref: 'use browser's Inspect tool to verify the exact errors'

I'm using typescript and code won't compile so I won't get to the browser till I can compile it.
0
 
SteHCommented:
Are both fname and lname defined?
0
 
Ryan ChongCommented:
so tried exactly what I posted previously?

use var and " instead of const and '
0
 
error2013Author Commented:
I had to add the parameters to another file. Your change worked fine :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.