最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Getting undefined value when I access value of an object - Stack Overflow

programmeradmin9浏览0评论

Inside loginUser function, I want to access the value of accessToken which is in side the 'stsTokenManager' but when i try to access this value then i am getting 'undefind' value.

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
// }

ponentDidMount() {
    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')
    }
}

Please check my loginUser function because i am able to access email and other think but when i try to access stsTokenManager then i am getting undefind value.And, I am using firebase authentication.

Inside loginUser function, I want to access the value of accessToken which is in side the 'stsTokenManager' but when i try to access this value then i am getting 'undefind' value.

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
// }

ponentDidMount() {
    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')
    }
}

Please check my loginUser function because i am able to access email and other think but when i try to access stsTokenManager then i am getting undefind value.And, I am using firebase authentication.

Share Improve this question edited May 31, 2018 at 10:12 Suresh Prajapati 4,5076 gold badges28 silver badges40 bronze badges asked May 31, 2018 at 9:59 suryasurya 1692 silver badges9 bronze badges 5
  • Can you log to check what is response.user? – Suresh Prajapati Commented May 31, 2018 at 10:01
  • Yes, it is return the response. – surya Commented May 31, 2018 at 10:05
  • So you mean response.user doesn't have key stsTokenManager ? – Suresh Prajapati Commented May 31, 2018 at 10:06
  • No, response.user have a key stsTokenManager but not access. – surya Commented May 31, 2018 at 10:09
  • Can you update the question with response value you are getting? Seems like you may have to parse before accessing the keys – Suresh Prajapati Commented May 31, 2018 at 10:14
Add a ment  | 

4 Answers 4

Reset to default 8

response.user.toJSON().stsTokenManager works.

response.user doesn't have an stsTokenManager property.

As a matter of fact, according to the documentation signInWithEmailAndPassword(email, password) returns a firebase.Promise containing a non-null firebase.auth.UserCredential.

In turn, the UserCredential contains a firebase.User (that you get through response.user as you did) which does not have such a stsTokenManager property, see https://firebase.google./docs/reference/js/firebase.User

Do not depend on internal undocumented properties that are subject to change. Instead use public and official APIs that are already available. In your case, you can use response.user.getIdToken() to get the user's ID token, this also ensures the token you get is fresh if the cached one is expired.

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

or

response.user.toJSON().stsTokenManager
发布评论

评论列表(0)

  1. 暂无评论