Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript firebase.auth().currentUser.uid-can';找不到未定义的uid_Javascript_Firebase_React Native_Firebase Realtime Database_Firebase Authentication - Fatal编程技术网

Javascript firebase.auth().currentUser.uid-can';找不到未定义的uid

Javascript firebase.auth().currentUser.uid-can';找不到未定义的uid,javascript,firebase,react-native,firebase-realtime-database,firebase-authentication,Javascript,Firebase,React Native,Firebase Realtime Database,Firebase Authentication,我的职能如下: recieveUserData = async() =>{ console.log(firebase.auth().currentUser.uid); let user =firebase.auth().currentUser.uid; await firebase.database().ref('/Users/'+user+'/Public').once('value').catch(error=>{console.log("error:" + error)}).

我的职能如下:

recieveUserData = async() =>{

console.log(firebase.auth().currentUser.uid);
let user =firebase.auth().currentUser.uid;
await firebase.database().ref('/Users/'+user+'/Public').once('value').catch(error=>{console.log("error:" + error)}).then((snapshot)=>{


    //Do something.
})

        }
控制台日志实际上生成uid。但是,需要变量“user”和“firebase.auth().currentUser.uid”的函数失败:

[Unhandled promise rejection: TypeError: Cannot read property 'uid' of null]. 
我认为调用firebase.auth()可能需要一些时间,因此后面的行会出错。我想我可以通过将上述内容更改为以下内容来解决此问题:

console.log(firebase.auth().currentUser.uid);
       await firebase.auth()
       .catch(err=>{console.log(err)})
       .then(response=>{console.log(response)});
我想我可以在.then()中写下后面的几行,但我得到了一个不同的错误:

Unhandled promise rejection: TypeError: _firebase.default.auth(...).catch is not a function]
我很困惑,因为我在另一个模块中使用了类似的东西,它似乎工作得很好。考虑到这些错误,我有点担心它现在会崩溃。任何洞察都非常方便


提前感谢

您不需要同时使用同步和异步


您不熟悉“
async
”和“
wait
”的用法。如果您理解“
sync
”,您可以看到代码的问题。您将其设置为“
async
”函数,并将“
wait
”放在firebase中。然后该函数从firebase开始,因此未定义“
user
”的值,并生成null。这会导致一个错误<“
的code>Promise
。然后”与“
异步
”和“
等待
”功能相同

你能试试这个密码吗

//异步
const{currentUser}=await firebase.auth()
让userUid=currentUser.uid
//同步
让userUid;
firebase.auth().then(currentUser=>{
userUid=currentUser.uid
})
.catch(错误=>console.log(错误))

您不需要同时使用同步和异步


您不熟悉“
async
”和“
wait
”的用法。如果您理解“
sync
”,您可以看到代码的问题。您将其设置为“
async
”函数,并将“
wait
”放在firebase中。然后该函数从firebase开始,因此未定义“
user
”的值,并生成null。这会导致一个错误<“
的code>Promise
。然后”与“
异步
”和“
等待
”功能相同

你能试试这个密码吗

//异步
const{currentUser}=await firebase.auth()
让userUid=currentUser.uid
//同步
让userUid;
firebase.auth().then(currentUser=>{
userUid=currentUser.uid
})
.catch(错误=>console.log(错误))

最后,我设法在父模块的ComponentDidMount中测试了我是否已通过身份验证,并且仅在我已通过身份验证的情况下才呈现相关模块。我还使用了firebase.auth().onAuthStateChanged,因为这将在我通过身份验证后立即更新,也就是说,它给了firebase足够的时间来返回实际的currentUser而不是null。这意味着我的初始代码/我尝试过的所有其他不同的解决方案现在都能实际工作:

class WholeProject extends Component {


state={
    user:null
}

componentDidMount = () => {
    firebase.auth().onAuthStateChanged((user)=>{
        if (user) {
            this.setState({ user });
        } else {
            this.setState({ user: null });
        }

    });}


render(){
let authenticated;
if (this.state.user === null)
authenticated=<Text>Unauthenticated</Text>;
else authenticated=<Profile user = {this.state.user}/>;


return(
<React.Fragment>
{authenticated}
</React.Fragment>

);

}
}

export default WholeProject;
类WholeProject扩展组件{
陈述={
用户:空
}
componentDidMount=()=>{
firebase.auth().onAuthStateChanged((用户)=>{
如果(用户){
this.setState({user});
}否则{
this.setState({user:null});
}
});}
render(){
让我们来验证;
if(this.state.user===null)
认证=未认证;
else认证=;
返回(
{authenticated}
);
}
}
导出默认的整个项目;

最后,我设法在父模块的ComponentDidMount中测试了我是否已通过身份验证,并且仅在我已通过身份验证的情况下才呈现相关模块。我还使用了firebase.auth().onAuthStateChanged,因为这将在我通过身份验证后立即更新,也就是说,它给了firebase足够的时间来返回实际的currentUser而不是null。这意味着我的初始代码/我尝试过的所有其他不同的解决方案现在都能实际工作:

class WholeProject extends Component {


state={
    user:null
}

componentDidMount = () => {
    firebase.auth().onAuthStateChanged((user)=>{
        if (user) {
            this.setState({ user });
        } else {
            this.setState({ user: null });
        }

    });}


render(){
let authenticated;
if (this.state.user === null)
authenticated=<Text>Unauthenticated</Text>;
else authenticated=<Profile user = {this.state.user}/>;


return(
<React.Fragment>
{authenticated}
</React.Fragment>

);

}
}

export default WholeProject;
类WholeProject扩展组件{
陈述={
用户:空
}
componentDidMount=()=>{
firebase.auth().onAuthStateChanged((用户)=>{
如果(用户){
this.setState({user});
}否则{
this.setState({user:null});
}
});}
render(){
让我们来验证;
if(this.state.user===null)
认证=未认证;
else认证=;
返回(
{authenticated}
);
}
}
导出默认的整个项目;

请不要在问题中包含答案的代码。如果你对某个答案有评论,请将该评论放在该答案下。它太大,无法放在评论lol中。请不要在你的问题中包含答案的代码。如果你对某个答案有评论,请将该评论放在该答案下。它太大,无法放在评论lol中。我根据你的答案将我的代码编辑为以下内容。代码有时是有效的。我不知道为什么会这样。recieveUserData=async()=>{const{currentUser}=await firebase.auth()让userUid=currentUser.uid;//console.log(userUid);await firebase.database().ref('/Users/'+userUid+'/Public')。一次('value')。捕获(错误=>{console.log(“错误:+error”)。然后((快照)=>{//DO SOMETHING});在一次尝试之后,我认为我的代码没有正确地实现wait。它在不等待的情况下运行下一行,因此,如果它在Firebase返回之前到达下一行,我将得到一个错误,但是如果Firebase速度更快,代码就会工作。是否可以将wait与Firebase.auth()一起使用出于某种原因?您不熟悉“async”和“await”的用法。如果您了解“sync”,您可以看到代码中的问题。您将其设置为“async”函数,并将“await”放在firebase中。然后该函数从firebase开始,因此未定义“user”的值,它生成null。这会导致错误。Promise“of”
.then
'与“
async
”和“
wait
”功能相同。@HarryShotta如果我的答案有帮助,请选择“V”符号和箭头作为答案,好吗?谢谢你的回答