我很难访问名为result
的分层对象中的某个属性:
result Object {
"additionalUserInfo": hg {
"isNewUser": true,
"providerId": "password",
},
"credential": null,
"operationType": "signIn",
"user": Object {
"apiKey": "some_api",
"appName": "[DEFAULT]",
"authDomain": "firebase.firebaseapp.com",
"createdAt": "1579042072524",
"displayName": null,
"email": "qp@email.com",
"emailVerified": false,
"isAnonymous": false,
"lastLoginAt": "1579042072524",
"phoneNumber": null,
"photoURL": null,
"providerData": Array [
Object {
"displayName": null,
"email": "qp@email.com",
"phoneNumber": null,
"photoURL": null,
"providerId": "password",
"uid": "qp@email.com",
},
],
"redirectEventId": null,
"stsTokenManager": Object {
"accessToken": "some_access_token",
"apiKey": "some_api_key",
"expirationTime": 1579045673373,
"refreshToken": "some_refresh_token",
},
"tenantId": null,
"uid": "some_uid",
},
}
我想访问“ some_access_token”,但是我只能访问result.user
,但是如果我做result.user && result.user.stsTokenManager
或
if(result.user.stsTokenManager){
console.log(result.user.stsTokenManager.accessToken)
}
我也尝试过const accessToken = (((result || {}).user || {}).stsTokenManager || {}).accessToken
但仍未定义
更新
这是我尝试访问变量但仍未定义的方式。
const submitHandler = async (input) => {
const { email, password } = input && input
let result;
try {
result = await Firebase.auth().createUserWithEmailAndPassword(email, password)
} catch (err) {
switch (err.code) {
case 'auth/email-already-in-use':
console.log("message", err.message)
}
}
const token = (((result || {}).user || {}).stsTokenManager || {}).accessToken
console.log("accessToken", token)
// or
if (result.user.stsTokenManager) {
console.log(result.user.stsTokenManager.accessToken)
}
// or
console.log(result && result.user.stsTokenManager.accessToken)
}
我什至尝试过lodash:
const accessToken = _.get(result, ['user', 'stsTokenManger', 'accessToken'])
但仍未定义
奇怪的是,我可以访问result.user.email
,result.user, emailVerified
,result.user.providerData
,result.user.uid
,但是我在undefined
,{ {1}},result.user.stsTokenManager
,即使我result.user.lastLoginAt
时也可以清楚地看到变量已填充。
答案 0 :(得分:0)
要解决您的问题,您必须在以下位置添加
: result = await Firebase.auth().createUserWithEmailAndPassword(email, password)
以下行:
const resData = await firebase.auth().currentUser.toJSON()
通过这种方式,您可以访问要查找的所有数据。
例如,现在您可以通过以下方式获取accessToken
:
resData.stsTokenManager.accessToken
...
resData.stsTokenManager.expirationTime