在访问对象的值时获取未定义的值

时间:2018-05-31 09:59:31

标签: javascript json firebase react-native firebase-authentication

在loginUser函数中,我想访问位于' stsTokenManager'旁边的accessToken的值。但是当我尝试访问这个值时,我会发现' unfind'值。

import React, { Component } from 'react';
import {
StyleSheet,
Text, 
View,
Image,
Button,
TouchableWithoutFeedback, StatusBar, TextInput,
SafeAreaView, Keyboard, TouchableOpacity,
KeyboardAvoidingView
} from 'react-native';
import { CheckBox, Item, Label, Input } from 'native-base';
import * as firebase from 'firebase';

const firebaseconfig={
apiKey: " ",
authDomain: " ",
databaseURL: " ",
projectId: " ",
storageBucket: " ",
messagingSenderId: ""
};
firebase.initializeApp(firebaseconfig);

export default class Login extends Component{
// static navigationOptions = {
//     header: null
// }

componentDidMount() {
    firebase.auth().onAuthStateChanged((user) => {
      if (user != null) {
        console.log(user)
      }
    })
}
loginUser = (email, password) => {         
    try {        
      firebase.auth().signInWithEmailAndPassword(email, password )
      .then((response)=>{
        //here i want to access values of 'stsTokenManager'
        console.log(response.user.stsTokenManager);
        }
    }
    catch (error) {
      alert('Check your email or password')
    }
}

请检查我的loginUser功能,因为我能够访问电子邮件和其他想法,但当我尝试访问stsTokenManager时,我得到unfind值。并且,我正在使用firebase身份验证。

4 个答案:

答案 0 :(得分:1)

response.user没有stsTokenManager属性。

事实上,根据documentation signInWithEmailAndPassword(email, password)返回包含非空firebase.Promise的{​​{1}}。

反过来,firebase.auth.UserCredential包含一个UserCredential(你通过firebase.User获得)并没有这样的response.user属性,请参阅{{3 }}

答案 1 :(得分:1)

dotnet pusblish有效。

答案 2 :(得分:0)

不要依赖可能会发生变化的内部无证件属性。而是使用已有的公共和官方API。在您的情况下,您可以使用response.user.getIdToken()来获取用户的ID令牌,这也可以确保如果缓存的令牌过期,您获得的令牌是新鲜的。

答案 3 :(得分:0)

JSON.parse(JSON.stringify(response.user)).stsTokenManager

response.user.toJSON().stsTokenManager