我的API出现离子401错误未经授权 这是我的Service Allet:我的httpService:我的typescript文件:我的路线: Controller函数:

问题描述

我将我的API集成到离子视图中,因此我想将我的汇款端点从一个叶子端口集成到另一个叶子端口,但出现401错误

这是我的源代码


离子源转移代码

  • 这是我的Service Allet

import { Injectable } from '@angular/core';
import { HttpService } from './http.service';
import { StorageService } from './storage.service';
import { Router } from '@angular/router';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class WalletService {

  constructor(
    private httpService: HttpService,private storageService: StorageService,private router: Router
  ) { }

  transfert(data: any): Observable<any>{
    return this.httpService.post("ewallet/transfer",data);
  }
}
  • 我的httpService

import { environment } from './../../environments/environment.prod';
import { Injectable } from '@angular/core';
import { HttpClient,HttpHeaders } from "@angular/common/http";
import { headersToString } from "selenium-webdriver/http";

@Injectable({
  providedIn: 'root'
})
export class HttpService {  
  httpOptions = {
    headers: new HttpHeaders({ 
      'Content-Type': 'application/json'
    }),withCredintials: false
  };

  constructor(
    private http: HttpClient
  ) {}

  post(serviceName: string,data: any){
    const url = environment.apiUrl + serviceName;
    return this.http.post(url,data,this.httpOptions);
  }

  getById(serviceName: string,id: string){
    const url = environment.apiUrl + serviceName;
    return this.http.get(url+"/"+id);
  }

  getAll(serviceName: string){
    const url = environment.apiUrl + serviceName;
    return this.http.get(url);
  }

  update(serviceName: string,id: string,data: any){
    const url = environment.apiUrl + serviceName;
    return this.http.put(url+"/"+id,this.httpOptions);
  }

  modify(serviceName: string,data: any){
    const url = environment.apiUrl + serviceName + id;
    return this.http.patch(url,this.httpOptions);
  }

  delete(serviceName: string,id: string){
    const url = environment.apiUrl + serviceName;
    return this.http.delete(url+"/"+id,this.httpOptions);
  }
}
import { ContactService } from './../services/contact.service';
import { AuthConstants } from 'src/app/config/auth-constant';
import { StorageService } from 'src/app/services/storage.service';
import { SuccessmodalPage } from './../modals/successmodal/successmodal.page';
import { Component,OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { ActivatedRoute } from '@angular/router';
import { AuthService } from '../services/auth.service';
import { WalletService } from '../services/wallet.service';

@Component({
  selector: 'app-requestreview',templateUrl: './requestreview.page.html',styleUrls: ['./requestreview.page.scss'],})
export class RequestreviewPage implements OnInit {
  public typee: any;
  public title: any;
  public amount: string;
  public contact: any;
  public authUser: any;
  public data: any = {
    name: '',email: ''
  };

  public dataTransfert = {
    amount: '',destinationAccountNumber: ''
  }

  constructor(
    public modalCtrl: ModalController,private route: ActivatedRoute,private authService: AuthService,private walletService: WalletService,private contactService: ContactService
  ) { }

  ngOnInit() {
    this.route.queryParams.subscribe(params => {
      this.typee = params["type"];
    });

    //get auth user @R_141_4045@ions
    this.authService.userData$.subscribe((res: any) =>{
      this.authUser = res;
      console.log(res.customer.accountNumber);
    });

    //get contact datas
    this.contactService.contactData$
      .subscribe(data => (this.contact = data));

    //get amount data
    this.contactService.amountData$
      .subscribe(data => (this.amount = data));
    //set title
    this.setTitle();

    console.log('données a transferer: ',this.getData());
  }

  getData(){
    this.dataTransfert.amount = this.amount;
    this.dataTransfert.destinationAccountNumber = this.contact.accountNumber;

    return this.dataTransfert;
  }

  transfert(){
    this.walletService.transfert(this.getData()).subscribe((res: any) =>{
      this.showModal();
    });
    
  }

  setTitle() {
    if (this.typee == 'request') {
      this.title = "Review and Request";
    }

    if (this.typee == 'send') {
      this.title = "Review and Send";
    }
  }

  async showModal() {
    const modal = await this.modalCtrl.create({
      component: SuccessmodalPage,backdropdismiss: true
    });

    return await modal.present();
  }
}

(在服务器上)我的node.js代码

  • 我的路线:

router
  .route('/transfer')
  /**
   * @api {post} v1/ewallet/transfer eWallet Transfer
   * @apiDescription Make a transfer to another eWallet
   * @apiVersion 1.0.0
   * @apiName Transfer
   * @apiGroup eWallet
   * @apiPermission customer
   *
   * @apiHeader {String} Authorization Customer's access token
   *
   * @apiParam  {Number{0...50000}}       amount       Decimal whith two fraction digits.
   * @apiParam  {Number}             destinationAccountNumber  Transaction's destinationAccountNumber
   *
   * @apiSuccess {Object}  transaction       Transaction.
   * @apiSuccess  {String}  transaction.id     Transaction's id
   * @apiSuccess  {Number}  transaction.accountNumber   Transaction's accountNumber
   * @apiSuccess  {Number}  transaction.destinationAccountNumber   Transaction's destinationAccountNumber
   * @apiSuccess  {String}  transaction.operation  Transaction's type of operation (deposit,withdrawal,transfer,fee)
   * @apiSuccess  {Number}  transaction.amount     Transaction's amount
   * @apiSuccess  {Number}  transaction.reference     Transaction's reference
   * @apiSuccess  {Date}    transaction.createdAt      Timestamp
   *
   * @apiSuccess {Object}  customer       Customer.
   * @apiSuccess  {String}  customer.id             Customer's id
   * @apiSuccess  {Number}  customer.accountNumber  Customer's accountNumber
   * @apiSuccess  {String}  customer.name           Customer's name
   * @apiSuccess  {String}  customer.email          Customer's email
   * @apiSuccess  {String}  customer.role           Customer's role
   * @apiSuccess  {Date}    customer.createdAt      Timestamp
   *
   * @apiError (Bad Request 400)   ValidationError  Some parameters may contain invalid values
   * @apiError (Unauthorized 401)  Unauthorized     Only authenticated customers can create the data
   * @apiError (Forbidden 403)     Forbidden        Only admins can create the data
   */
  .post(authorize(),validate(walletTransfer),controllerWallet.transfer); //authorize(),walletTransfer,
/**
 * eWallet Transfer
 * @public
 */
exports.transfer = async (req,res,next) => {
  try {    
    const transferResponse = await transferService.transfer(req.customer.accountNumber,req.body.amount,req.body.destinationAccountNumber);    
    res.json(transferResponse);    
    
  } catch (error) {
    next(error);
  }
};

感谢您的帮助,谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)