在此POST方法中,我正在执行console.log( 'value:- '+ this.newCourses);
时正在发布数据这里对象对象来了,这里我发布的值不打印
{ new course data saved succcesfully ,
this.token ... [object Object] ,
value:- [object Object] }
postData(){
this.token = getToken();
this.details['title'] = this.form.getRawValue();
this.details['category'] = this.form.getRawValue();
this.details['length'] = this.form.getRawValue();
this.details['content'] = this.form.getRawValue();
this.details['product_name'] = getProduct()['name'];
this.details['updated'] = 'Monday';
console.log(this.details);
this.httpHeaders = new HttpHeaders({
"Authorization": "Bearer " + this.token});
this._httpClient.post('http://127.0.0.1:8000/api/products/add-course/',this.details, {headers: this.httpHeaders}).subscribe(
result => {
console.log(result);
console.log("new course data saved succcesfully");
this.token = getToken();
this.newCourses = result;
console.log( 'value:- '+ this.newCourses);
},
error => {
console.log(error);
}
);
}
您只看到将对象连接到字符串的结果。
如果要记录对象本身,请分别记录字符串和对象:
console.log('value:- ', this.newCourses);
注意,我在这里将2个参数传递给console.log
。
当连接对象和字符串时,将调用对象的toString()
方法。默认情况下,它将显示[object Object]
。
const obj = { a: 1, b: 2, c: 3 };
// concatenate obj with string
console.log('value: - ' + obj);
// log string and object itself as separate args
console.log('value: - ', obj);
// log obj.toString()
const objString = obj.toString();
console.log(objString);
奖金编辑:
如果需要,您可以覆盖toString()
:
// override toString()
const obj = { a: 1, b: 2, c: 3, toString: () => 'World!' };
console.log('Hello, ' + obj);
`const headers = new HttpHeaders().set(
"security-token",
localStorage.getItem("token")
);
const httpOptions = {
headers: headers,
observe: 'response' as 'response',
params: data
};
return this.http
.get<any>("/url/", httpOptions)
.pipe(
map(response => {
return response
}),
catchError(this.handleError)
);`