可能的未处理的承诺拒绝(id:0):错误缺少baseUrl

时间:2018-11-27 13:44:35

标签: javascript reactjs react-native

每次我在本机应用程序上按下特定按钮时,都会收到此错误。我猜想它必须通过Api调用来做某事,或者它正在尝试使用localhost:8081调用URL,这可能是不正确的,因为它应该指向服务器。我不确定确切如何找出问题所在。任何帮助将非常感激。现在,我也可以确定问题是否出在我在下面共享的文件中。

应用警告消息:

App Warning

这是我的RestClient代码:

import {Alert, AsyncStorage} from 'react-native'
import {resetRouteTo} from 'util/NavigationHelper'
import {store} from '../index.js'
import {dropdownAlert} from 'util/AlertManager'

const DEFAULT_ERROR = {error: {code: 500, message: 'No JSON message'}}

export default class RestClient {
  constructor (baseUrl = '', navigation, { headers = {}, devMode = false, simulatedDelay = 0 } = {}) {
    if (!baseUrl) throw new Error('missing baseUrl')
    this.navigation = navigation
    this.headers = {
      'Accept': 'application/json',
      'Content-Type': 'application/json'
    }
    Object.assign(this.headers, headers)
    this.baseUrl = baseUrl
    this.simulatedDelay = simulatedDelay
    this.devMode = devMode
  }

  _clearTokenAndGo () {
    AsyncStorage.removeItem('apiToken')
    Alert.alert(
      'Error',
      'Something went wrong and your account needs to be re-instantiated.',
      [
        {text: 'OK', onPress: () => resetRouteTo(this.navigation, 'OnboardingFirst')}
      ],
      { cancelable: false }
    )
  }

  _simulateDelay () {
    return new Promise(resolve => {
      setTimeout(() => {
        resolve()
      }, this.simulatedDelay)
    })
  }

  async _parseIfJson (response) {
    var contentType = response.headers.get('content-type')
    if (contentType && contentType.indexOf('application/json') !== -1) {
      return response.json()
    }
    return null
  }

  async _handleError (response) {
    const body = await this._parseIfJson(response)
    if (!body) {
      dropdownAlert('error', `Server Error ${response.status}`, 'Something went wrong on the server')
      return DEFAULT_ERROR
    }
    switch (response.status) {
      case 200: {
        break
      }
      case 401: {
        if (body.error === 'Unauthenticated.') {
          this._clearTokenAndGo()
        }
        break
      }
      case 400: {
        dropdownAlert('error', `Error ${body.error.code}`, body.error.message)
        break
      }
      default: {
        if (body.error) {
          dropdownAlert('error', `Error ${body.error.code}`, body.error.message)
        } else {
          dropdownAlert('error', 'Error', `An unknown error has occurred. Http status ${response.status}`)
        }
        break
      }
    }
    return body
  }

  _fullRoute (url) {
    return `${this.baseUrl}${url}`
  }

  async _fetch (route, method, body, isQuery = false) {
    if (!route) throw new Error('Route is undefined')
    if (!store.getState().netinfo.isConnected) {
      this.navigation.navigate('BlockScreen')
      return {success: false, error: {code: 1, message: 'No internet connection.'}}
    }
    var fullRoute = this._fullRoute(route)
    if (isQuery && body) {
      var qs = require('qs')
      const query = qs.stringify(body)
      fullRoute = `${fullRoute}?${query}`
      body = undefined
    }
    let opts = {
      method,
      headers: this.headers
    }
    if (body) {
      Object.assign(opts, { body: JSON.stringify(body) })
    }
    const fetchPromise = () => fetch(fullRoute, opts)
    if (this.devMode && this.simulatedDelay > 0) {
      // Simulate an n-second delay in every request
      return this._simulateDelay()
        .then(() => fetchPromise())
        .then(response => response.json())
    } else {
      let promise = await fetch(fullRoute, opts)
      console.log('Logging response =>')
      console.log(promise)
      return this._handleError(promise)
    }
  }

  GET (route, query) { return this._fetch(route, 'GET', query, true) }
  POST (route, body) { return this._fetch(route, 'POST', body) }
  PUT (route, body) { return this._fetch(route, 'PUT', body) }
  DELETE (route, query) { return this._fetch(route, 'DELETE', query, true) }
}

使用RestClient的其他文件:

import RestClient from 'util/RestClientLib'
import qs from 'qs'
import { Linking, AsyncStorage } from 'react-native'
import Config from 'react-native-config'
var SHA256 = require('crypto-js/sha256')

export default class ApiClient extends RestClient {
  constructor (authToken, navigation) {
    console.log('constructing apiClient with base: ', Config.API_URL)
    super(Config.API_URL, navigation, {
      headers: {
        'Authorization': 'Bearer ' + authToken
      }
    })
  }

  _switchSchemeTo (url, scheme) {
    var split = url.split(':')
    split[0] = scheme
    return split.join(':')
  }

  _makeAppUrl (url) {
    const prefix = url.split('.')[0]
    const bank = prefix.substr(prefix.lastIndexOf('/') + 1, prefix.length)
    switch (bank) {
      case 'florijnbank':
        return this._switchSchemeTo(url, 'flrb')
      default:
        return url
    }
  }

  async _openUrl (url) {
    const openInApp = await AsyncStorage.getItem('openInApp')
    const appUrl = this._makeAppUrl(url)
    Linking.canOpenURL(appUrl).then(supported => {
      if (!supported || openInApp !== 'true') {
        Linking.canOpenURL(url).then(supported => {
          if (!supported) {
            console.log('Can\'t handle url: ' + url)
          } else {
            Linking.openURL(url)
          }
        }).catch(err => console.error('An error occurred', err))
      } else {
        Linking.openURL(appUrl)
      }
    }).catch(err => console.error('An error occurred', err))
  }

  async createToken (pin) {
    var hash = SHA256(pin).toString()
    const query = {pincode: hash}
    let response = await this.POST('/user', query)
    return response.api_token
  }

  async checkPin (pin) {
    const hash = SHA256(pin).toString()
    const query = {pincode: hash}
    return this.GET('/user/validate', query)
  }

  getAccounts () {
    return this.GET('/account')
  }

  getBalance (iban) {
    return this.GET(`/account/${iban}`)
  }

  async getPermissionBank (bank) {
    const query = {
      bank_id: bank
    }
    let response = await this.POST('/account', query)
    return this._openUrl(response.url)
  }

  postCredentialsAis (form) {
    return this.POST('/account/password', form)
  }

  async ais (iban) {
    let response = await this.GET(`/ais/${iban}`)
    if (response.url == null) {
      return true
    }
    this._openUrl(response.url)
    return false
  }

  getTransactionDetails (requestId) {
    return this.GET(`/request/${requestId}`)
  }

  getTransactions (iban) {
    return this.GET(`/account/${iban}/transaction`)
  }

  getContacts () {
    return this.GET('/contacts')
  }

  getSettings () {
    return this.GET('/startup')
  }

  deleteAccount (iban) {
    return this.DELETE(`/account/${iban}`)
  }

  /**
   * async submitTransfer - submits a transfer
   *
   * @param  {Object} options  object which holds the pin, iban, sso and query
   * @param  {Object} transfer hold the transfer information
   * @return {Boolean}          either opens a link or a boolean for success
   */
  submitTransfer (iban, credentials, transfer) {
    const url = `/account/${iban}/transaction?${qs.stringify(credentials)}`

    const body = {
      counter_iban: transfer.counterIban,
      counter_account_name: transfer.name,
      amount: transfer.amount,
      description: transfer.description,
      reference: transfer.reference
    }

    return this.POST(url, body)
  }

  qrConfirmTransfer (transactionId, iban, credentials) {
    const url = `/request/${transactionId}/confirm/${iban}?${qs.stringify(credentials)}`
    return this.POST(url)
  }

  directConfirmTransfer (transactionId, iban, form) {
    const url = `/account/${iban}/transaction/${transactionId}`
    return this.POST(url, form)
  }

  verificationRequired (iban, amount) {
    const query = {amount: amount}
    return this.GET(`/veriReq/${iban}`, query)
  }
};

0 个答案:

没有答案
相关问题