0
ユーザーのFirebase ID( "id": "1234567890")を同じデータベース内の別のデータセットのキーとして利用するにはどうすればよいですか?FirebaseのFacebookログインIDをデータキーとしてRepurposeしますか?
以下は、私がからIDを引くでしょう、私のログイン詳細を次のようになります。
admin.database().ref('location_config/{id}').set({current_location:[34.047220, -118.443534]})
結果が現在のように出てくる:として私は現在、私のコードを持ってFirebase関数内
"users": {
"1234567890abcdefghijklmnopqrstuvwxyz" : {
"dp" : "https://www.profilePic.com",
"first_name" : "John",
"id" : "1234567890",
"last_name" : "Doe",
"token" : "abcdefghijklmnopqrstuvwxyz1234567890",
"uid" : "987654321"
}
}
:
"location_config": {
"{id}": {
"current_location": [34.047220, -118.443534]
}
}
これは、データがIDで表示されるようにする方法です:
"location_config": {
"1234567890": {
"current_location": [34.047220, -118.443534]
}
}
次のスクリーンショットは、IDが一定の間にUIDがどのように動的であるかを示しています。ここで
Firebase内のコードです:
let fbLocation;
module.exports = (event) => {
event.geoFire = functions.database.ref('users').onUpdate(event => {
admin.database().ref('/portal_coordinates_all').once('value', snapshot =>{
fbLocation = snapshot.val();
console.log ("snapshot", fbLocation);
}).then(() => {
// Create a Firebase reference where GeoFire will store its information
let firebaseRef = admin.database().ref('geofire');
// Create a GeoFire index
let geoFire = new GeoFire(firebaseRef);
geoFire.set(fbLocation)
.then(() => {
console.log("Provided key has been added to GeoFire");
}).catch(err => console.log(err))
.then(() => {
let geoQuery = geoFire.query({
center: [34.047220, -118.443534],
radius: 2
});
let locations = [];
let onKeyEnteredRegistration = geoQuery.on("key_entered", function(key, location, distance) {
locations.push(location);
});
// fires once when this query's initial state has been loaded from the server.
let onReadyRegistration = geoQuery.on("ready", function() {
console.log("GeoQuery has loaded and fired all other events for initial data");
console.log(locations);
// ******* here is where I'm having the issue *******
admin.database().ref('location_config/`${id}`').set({current_location: locations})
// **************************************************
// Cancel the "key_entered" callback
onKeyEnteredRegistration.cancel();
});
}).catch(err => console.log(err))
})
})
}
そしてここでは、コードが内部ネイティブリアクトさ:
import React, { Component } from 'react';
import {
StyleSheet,
View,
ActivityIndicator,
Button
} from 'react-native';
import firebase from 'firebase';
import { connect } from 'react-redux';
import { loginSuccess } from '../actions/AuthActions';
const FBSDK = require('react-native-fbsdk');
const { LoginManager, AccessToken } = FBSDK;
class Login extends Component {
constructor(props) {
super(props);
this.state = {
showSpinner: true,
};
}
componentDidMount() {
this.fireBaseListener = firebase.auth().onAuthStateChanged(auth => {
if (auth) {
this.firebaseRef = firebase.database().ref('users');
this.firebaseRef.child(auth.uid).on('value', snap => {
const user = snap.val();
if (user != null) {
this.firebaseRef.child(auth.uid).off('value');
this.props.loginSuccess(user);
}
});
} else {
this.setState({ showSpinner: false });
}
});
}
onPressLogin() {
this.setState({ showSpinner: true })
LoginManager.logInWithReadPermissions([
'public_profile',
'user_birthday',
'email',
'user_photos'
])
.then((result) => this.handleCallBack(result),
function(error) {
alert('Login fail with error: ' + error);
}
);
}
handleCallBack(result) {
let that = this;
if (result.isCancelled) {
alert('Login canceled');
} else {
AccessToken.getCurrentAccessToken().then(
(data) => {
const token = data.accessToken
fetch('https://graph.facebook.com/v2.8/me? fields=id,first_name,last_name&access_token=' + token)
.then((response) => response.json())
.then((json) => {
const imageSize = 120
const facebookID = json.id
const fbImage = `https://graph.facebook.com/${facebookID}/picture?height=${imageSize}`
this.authenticate(data.accessToken)
.then(function(result) {
const { uid } = result;
that.createUser(uid, json, token, fbImage)
});
})
.catch(function(err) {
console.log(err);
});
}
);
}
}
authenticate = (token) => {
const provider = firebase.auth.FacebookAuthProvider;
const credential = provider.credential(token);
return firebase.auth().signInWithCredential(credential);
}
createUser = (uid, userData, token, dp) => {
const defaults = {
uid,
token,
dp
};
firebase.database().ref('users').child(uid).update({ ...userData, ...defaults });
}
render() {
return (
this.state.showSpinner ? <View style={styles.container}><ActivityIndicator animating={this.state.showSpinner} /></View> :
<View style={styles.container}>
<Button
onPress={this.onPressLogin.bind(this)}
title="Login with Facebook"
color="#841584"
/>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
}
});
const mapStateToProps = (state) => {
console.log('mapStateToProps', state);
return {
logged: state.auth.loggedIn,
user: state.auth.user
};
};
export default connect(mapStateToProps, { loginSuccess })(Login);
あなたは文字通りのテンプレートにバッククォートを使うと '$'プレフィックスを使用する必要があります。 '' admin.database()REF( 'location_config/$ {ID}')を ''。これはまた、 'id'があなたの関数に正しい値を持っていることを前提としています。 –
@ frank-van-puffelen私は 'id'の値が(1234567890)であると定義していないのでエラーになります。私はスナップショットをつかみ、それを変数「id」にしてから、それがうまくいくように指を渡そうとしています。 –
IDを関数をトリガーするユーザーのUIDにする場合は、https://stackoverflow.com/questions/42750060/getting-the-user-id-from-a-database-trigger-in-cloudを参照してください。 -functions-for-firebase。質問を編集して、あなたがどこにいるのかを再現する[最小*完全なコード](http://stackoverflow.com/help/mcve)を含めると、助けが簡単になるわけではありません。私たち(または少なくとも私)が非常に効果的であるようにするには、あまりにも多くの推測が必要です。フランク・ヴァン・puffelen @ –