数据未保存在数据库中

时间:2018-06-17 21:08:42

标签: javascript node.js angular html5 express

我有api终点将数据保存到数据库,发布/评论/ 当我在邮递员中测试它时,一切都很完美。但是前端数据被保存但只有id,我想从表单输入作者和评论中保存的数据。

这是用户提交表单输入后保存在数据库中的内容:

{
    "_id": {
        "$oid": "5b26c65ffecfe0047846677d"
    },
    "author": null,
    "description": null
}

这是component.ts:

import { Component, OnInit} from '@angular/core';
import { MoviesService } from '../movies.service';
import { RouterModule, Routes } from '@angular/router';
import {ActivatedRoute} from '@angular/router';

@Component({
  selector: 'app-movie',
  templateUrl: './movie.component.html',
  styleUrls: ['./movie.component.scss']
})
export class MovieComponent implements OnInit {
  movie: object;
  review: {};
  addreview: any;
  addreviews: any[];
  constructor(private router: ActivatedRoute, private moviesService: MoviesService) {
    this.movie = [];
    this.review = [];
    this.addreview = [];
    this.addreviews = [];
  }
  addReview(addreview: any): void {
    if (!addreview) { return; }
    this.moviesService.createReview(addreview)
      .then(reviews => {
       console.log(reviews);
       this.addreviews.push(reviews.addreview);
      });
  }
  ngOnInit() {
        this.router.params.subscribe((params) => {
          // tslint:disable-next-line:prefer-const
          let id = params['id'];
          this.moviesService.getMovie(id)
           .then(movie => {
            this.movie = movie;
          });
        });

        this.router.params.subscribe((params) => {
          // tslint:disable-next-line:prefer-const
          let id = params['id'];
          this.moviesService.getReview(id)
           .then(review => {
             console.log(review);
            this.review = review;
          });
        });
      }
 }

这是service.ts

export class MoviesService {
  searchStr: string;
  queryUrl = '/search/';
  theatreUrl = '/theatre/';
  moveUrl = '/movies/';
  reviewUrl = '/comments/';
  private apiUrl = 'http://localhost:8000';
  constructor(private http: Http, private _jsonp: Jsonp) { }

  createReview(review: string): Promise<any> {
    return this.http.post(this.apiUrl + this.reviewUrl, review)
               .toPromise()
               .then(this.handleData)
               .catch(this.handleError);
  }

  private handleData(res: any) {
    const body = res.json();
    console.log(body); // for development purposes only
    return body.result || body || {};
  }
  private handleError(error: any): Promise<any> {
    console.error('An error occurred', error); // for development purposes only
    return Promise.reject(error.message || error);
  }
}

compo.html

<form (ngSubmit)="addReview(addreview)">
            <div class="form-group movie-username">
              <label for="author">Name:</label>
              <input type="text" class="form-control movie-username_text" id="usr" [(ngModel)]="addreview.author" name="author">
            </div>
            <div class="form-group movie-textarea">
              <label for="movie-textarea_label description">Do you like the movie? leave us a comment below</label>
              <textarea class="form-control rounded-0" 
              id="movie-textarea_textarea" #description="ngModel" rows="10"
              [(ngModel)]="addreview.description" 
              name="description"></textarea>
            </div>
            <button type="submit" class="btn btn-primary">Submit</button>
          </form>

提交的数据没有保存到数据库我的代码出了什么问题?任何帮助都会被贬低,谢谢

0 个答案:

没有答案