API session connection issue

IRn00b3 years ago

I'm attempting to connect to my traccar server via API using react-native, at this point just attempting to capture a session response. However the API session attempts appear to break the DB somehow.. I can utilize my traccar setup without any issues or errors when using the webapp or the android manager app, so this is very strange to me..

Here is my basic code:

import React from 'react';
import {StyleSheet, Text, View, Button, ScrollView, TouchableOpacity} from 'react-native';

export default class App extends React.Component {
  constructor() {
    super();
    this.state = {
      data: null,
      loaded: true,
      error: null,
    };
  }
  baseURL = 'https://<myurl>';
  email = '<myemail>';
  password = '<mypass>';

  getData = (ev) => {
    this.setState({loaded: false, error: null});
    let url = this.baseURL + '/api/session';

    fetch(url, { method: 'POST', body: new URLSearchParams(`email=${this.email}&password=${this.password}`) })
      .then(response => response.text())
      .then(this.showData)
      .catch(this.badStuff);
  };
  showData = (data) => {
    this.setState({loaded: true, data});
    console.log(data);
  };
  badStuff = (err) => {
    this.setState({loaded: true, error: err.message});
  };
  componentDidMount() {
    //this.getData();
    //geolocation -> fetch
  }
  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.txt}>Gimme some data!</Text>
        <View style={styles.buttonContainer}>
          <TouchableOpacity>
            <Button title="Get Data" onPress={this.getData} />
          </TouchableOpacity>
        </View>
        {!this.state.loaded && <Text>LOADING</Text>}
        {this.state.error && <Text style={styles.err}>{this.state.error}</Text>}
        <ScrollView>
          <Text style={styles.txt}>{this.state.data}</Text>
        </ScrollView>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
  },
  txt: {
    fontSize: 24,
    color: '#333',
    padding: 20,
  },
  err: {
    color: 'red',
    fontSize: 30,
    fontWeight: 'bold',
    padding: 20,
  },
  buttonContainer: {
    height: 40,
    width: 100,
  },
});

RESPONSE
When attempting to session using the above code, the first few responses I get are:

NullPointerException (DataManager:318 < PermissionsManager:439 < SessionResource:104 < ...)

After about 4-5 API attempts, the DB seems to lock up, and I get this response (to clear I have to restart traccar service on server):

HikariPool-1 Connection is not available, request timed out after 30000ms.- SQLTransientConnectionException(...< QueryBuilder:59 < *:131 < DataManager:317 < PermissionsManager:439 < ...) 

NOTES
-I have my Traccar server hosted on an ubuntu GCP and it's DB is a GCP PostgreSQL 12 (db = traccar, collation = en_US.UTF8, character set = UTF8). I followed the documentation here to configure the traccar server.
-Traccar server connects fine to the PostgreSQL and I can use all features from either the webapp or android manager app. Also I have multiple devices connected and all working fine.
-The only time I see DB errors in the server log files is when attempting API session using the code above.

Anton Tananaev3 years ago

I would recommend comparing your API requests with the ones that official web app sends. There must be some difference.

IRn00b3 years ago

Right.. I used this login POST as an example.

Anton Tananaev3 years ago

I was talking about actual request, not the code.