0

I am trying to pass values to formData from a form in my view, but when I am passing my parameters, it is showing undefined.

    // Initialize Params Object
    let myFormData = new FormData();

    // Begin assigning parameters     
    const username = myFormData.append('myUsername', this.registerForm.value.firstname);
    const email = myFormData.append('myEmail', this.registerForm.value.email);

    //post request
    return this.http.post('http://localhost:81/save.php/', myFormData).subscribe((res: Response) => {
        console.log(res);
    },
    (err) =>{
        this.indexedDbService
            .addUser(username,email)  //  undefined , undefined
            .then(this.backgroundSync)
            .catch(console.log);
            // this.backgroundSync();
    })
} 

However, if I try passing a hard-coded JSON object, it is taking properly. How can I pass data from myFormData into the parameters of addUser properly?

1 Answer 1

1

FormData.append is a void function, so username and email will always be undefined in your code.

const example = new FormData();
const voidResult = example.append('foo', 'bar');
console.log(voidResult) // undefined

You'll need to assign those to your form values instead if you want to access them that way. Example:

const { firstname: username, email } = this.registerForm.value;
myFormData.append('myUsername', username);
myFormData.append('myEmail', email);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the information. Didn't noticed it is void function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.