#Why am I getting empty data when doing JSON.stringify(model)

21 messages · Page 1 of 1 (latest)

fiery storm
#

My model basically contains the data of a form. after stringify I am getting empty data as {}. Why is this happening?

#
// your code here
claimKey
: 
"PCI0300147"
scode
: 
"1505"
selcom
: 
"test sid"
seven
: 
"Test hk"
teamCode
: 
"999"
ty
: 
"BODY"
userName
: 
"sprager"
#

data inside my model

gilded thistle
#

Post the code you're using.

fiery storm
#
// your code here
createComments(model: CommentsDto | null | undefined): Observable<void> {
        let url_ = this.baseUrl + "some data"; //ignore this

        url_ = url_.replace(/[?&]$/, "");

        const content_ = JSON.stringify(model);

        let options_ : any = {
            body: content_,
            observe: "response",
            responseType: "blob",
            headers: new HttpHeaders({
                "Content-Type": "application/json",
            })
        };

        return this.http.request("post", url_, options_).pipe(_observableMergeMap((response_ : any) => {
            return this.processCreateComments(response_);
        })).pipe(_observableCatch((response_: any) => {
            if (response_ instanceof HttpResponseBase) {
                try {
                    return this.processCreateComments(response_ as any);
                } catch (e) {
                    return _observableThrow(e) as any as Observable<void>;
                }
            } else
                return _observableThrow(response_) as any as Observable<void>;
        }));
    }
gilded thistle
#

Where are you logging content_?

fiery storm
#

forget that.

#

In content_ I am not able to get the data

gilded thistle
#

How do you know that?

fiery storm
#

I just logged it on console.

fiery storm
gilded thistle
#

And again: where do you put the log statement?

fiery storm
#

my friend. doing console.log(content_) is same as JSON.stringify(model)

gilded thistle
#

Maybe is the same as console.log(JSON.stringify(model))
And for the third time: where are you inserting the log?

fiery storm
#

I am putting the content_ inside options and then hitting the api

fiery storm
#

Wouldyou please just tellme why am i not getting any value

gilded thistle
#

Please, turn this part of your code

const content_ = JSON.stringify(model);

into this

const content_ = JSON.stringify(model);
console.log(content_)

And post what you get in console.

gilded thistle
#

If it returns empty again, post how you generate the object you pass as model argument to your createComments function.

fiery storm
#

got it now. before I was adding all the values from the form to the model directly and then sending the model in api. Now, I passed the data parameters separately in api and it worked.