将数据从视图发送到apiservice以进行用户ID验证

时间:2018-11-23 13:11:07

标签: typescript angular6 angular-services angular-forms angular-validation

我在Spring Boot上使用angular 6.我有两个模型用户和post。我只想只允许现有的用户发布post。我有两个模型:User和Post。 我在服务中尝试了这种方法:

 getUserIdfromUsers(id: number): any {


    // tslint:disable-next-line:no-shadowed-variable
    const promise = new Promise((resolve, reject) => {
      return this.http.get( this.GET_USERS + '/list').subscribe(
        respose => {
          this._user_id = respose.data.find(x => x.id === id);
//id is referred to posts.userId
              return resolve(this._user_id);
            }, (err: HttpErrorResponse) => {
              return reject(err);
            });

    });
    return promise;
  }

我在组件文件中尝试了此方法

save() {
    this.apiService.createPost(this.posts)

      // tslint:disable-next-line:no-shadowed-variable
      .subscribe(data => (console.log(data)), error => console.log(error));
    this.apiService.getUserIdfromUsers(this.posts.userId); // I tried like this

    this.posts = new Post();

  }

但是它没有用。我该如何解决这个问题?

0 个答案:

没有答案