Friday, 7 April 2017

TypesScript: unable to read a property of an object even though it's there

I'm trying to create a body and send my url to the backend where it stores my content objects. However when I use const body = JSON.stringify(content); It doesn't read the property file.

Here are is my service code:

addMessage(content: Content) {
    console.log("Service:", content); // File property is in the content object I sent
    const body = JSON.stringify(content); // File property missing here
    console.log("Body:", body);
    const headers = new Headers({'Content-Type': 'application/json'});
    const token = localStorage.getItem('token')
        ? '?token=' + localStorage.getItem('token')
        : '';
    return this.http.post('http://localhost:3000/content' + token, body, {headers: headers})
        .map((response: Response) => {
            const result = response.json();
            const newContent = new Content(
                result.obj.name,
                result.obj.user.firstName,
                result.obj._id,
                result.obj.user._id,
            );
            this.contents.push(newContent);
            return newContent;
        })
        .catch((error: Response) => {
            this.errorService.handleError(error.json());
            return Observable.throw(error.json());
        });
}

Here's my console:

enter image description here

You can see that file is in content but doesn't appear in body. I tried doing JSON.parse(JSON.stringify(content)); and to my surprise, file wasn't there. Any idea what could've went wrong?

Thank you!



via YSA