skip to Main Content

I am developing an app by react native build by Expo.

I try to register and update some columns, but these don’t work well.

My sourcecode is below

import * as Location from 'expo-location';
const URL = {
    CREATE_CURRENT_LOCATION:'https://sayhellotobike-native-ios-default-rtdb.firebaseio.com'
}

export const requestLocationPermission = async (setLocationCallBack,myCurrentLocationArray,setIsMapReady) => {
try {
    const { status } = await Location.requestForegroundPermissionsAsync();
    if (status === 'granted') {
        // 位置情報の取得処理を実行する
        getCurrentLocation(setLocationCallBack,myCurrentLocationArray,setIsMapReady);
        
    } else {
        console.log('Location permission denied');
    }
    } catch (error) {
        console.error('Error requesting location permission:', error);
    }
};

export const getCurrentLocation = async (setLocationCallBack,myCurrentLocationArray,setIsMapReady) => {
    try {
        const { coords } = await Location.getCurrentPositionAsync({});
        const { latitude, longitude } = coords;
        setLocationCallBack({ ...myCurrentLocationArray,latitude, longitude });
        setIsMapReady(true); // マップが準備完了
        writeMyLocationData(URL.CREATE_CURRENT_LOCATION,0,latitude, longitude )
        } catch (error) {
            console.error('Error getting current location:', error);
        } 
};

import { ref, set } from "firebase/database";
import { database } from '../firebaseConfig'; // Firebaseのデータベースを正しくインポートする必要があります
export const writeMyLocationData = async (url, userId, latitude, longitude) => {
  console.log("書き込み開始:", url, userId, latitude, longitude);
  let params = {
      user_id: userId,
      latitude: latitude,
      longitude: longitude[enter image description here](https://i.sstatic.net/3GnaeJSl.png)
    }
  try{
    set(ref(database, url + 'location'), params
  )
  }catch(error){
    console.error(error)
  }finally{
    console.log("処理を送りました。")
  }
  console.log("fetchLocation",fetchLocation)
  // .then(() => {
  //   console.log("書き込み成功:", url, userId, latitude, longitude);
  // })
  // .catch((error) => {
  //   console.error("書き込みエラー:", error);
  // });
}

I guess the path might be wrong special characters.

Error code is below. I am sure i dont use like.

Error: child failed: path argument was an invalid path = "https:your text//sayhellotobike-native-ios-default-rtdb.firebaseio.com/location/latitude". Paths must be non-empty strings and can’t contain ".", "#", "$", "[", or "]"

I tried to change the path like

/location 
/location/latitude 
location
/

and so on.

import { initializeApp } from 'firebase/app';
import { getAuth } from "firebase/auth"
import { getDatabase }from "firebase/database";

const firebaseConfig = {
  apiKey: "AIzaSyCe2ZqMbYldfc-M7hXEBBSNJHnJy5gMig4",
  authDomain: "sayhellotobike-native-ios.firebaseapp.com",
  databaseURL: "https://sayhellotobike-native-ios-default-rtdb.firebaseio.com",
  projectId: "sayhellotobike-native-ios",
  storageBucket: "sayhellotobike-native-ios.appspot.com",
  messagingSenderId: "94237205998",
  appId: "1:94237205998:web:879feeef3ddb781d3e1aff",
  measurementId: "G-ZLR4NQ9XG4"
};
const app = initializeApp(firebaseConfig);
export const auth = getAuth(app);
export const database = getDatabase(app);

2

Answers


  1. Chosen as BEST ANSWER
    import { initializeApp } from 'firebase/app';
    import { getAuth } from "firebase/auth"
    import { getDatabase }from "firebase/database";
    
    const firebaseConfig = {
      apiKey: "AIzaSyCe2ZqMbYldfc-M7hXEBBSNJHnJy5gMig4",
      authDomain: "sayhellotobike-native-ios.firebaseapp.com",
      databaseURL: "https://sayhellotobike-native-ios-default-rtdb.firebaseio.com",
      projectId: "sayhellotobike-native-ios",
      storageBucket: "sayhellotobike-native-ios.appspot.com",
      messagingSenderId: "94237205998",
      appId: "1:94237205998:web:879feeef3ddb781d3e1aff",
      measurementId: "G-ZLR4NQ9XG4"
    };
    const app = initializeApp(firebaseConfig);
    export const auth = getAuth(app);
    export const database = getDatabase(app);
    

  2. The problem you have is that you are using ref(db, path) from the firebase/database package instead of refFromURL(db, url).

    On the following line, you incorrectly provide a URL to the ref function when it expects a path.

    ref(database, url + 'location')
    

    The reason you get an error is because of the constraint applied to the path variable that blocks the periods (".") in the domain:

    Paths must be non-empty strings and can’t contain ".", "#", "$", "[", or "]"

    To use a URL, you should use refFromURL instead.

    refFromURL(database, url + 'location');
    

    However, based on the value of URL.CREATE_CURRENT_LOCATION you need to either use url + '/location' or new URL('location', url).toString() to correctly construct the URL. Currently the URL would be "https://<db>.firebaseio.comlocation" instead of "https://<db>.firebaseio.com/location".

    refFromURL(database, url + '/location');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search