角度发布方法无法正常工作

时间:2020-03-05 07:45:11

标签: angular service crud

在这种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);
                                }
                            );

     }

1 个答案:

答案 0 :(得分:3)

您刚刚看到将对象连接到字符串的结果。

如果要记录对象本身,请分别记录字符串和对象:

console.log('value:- ', this.newCourses);

注意,我在这里将两个参数传递到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);