2017-10-31 4 views
0

몇 시간 동안 검색 한 결과 이와 관련된 단일 항목을 찾을 수 없습니다.Express에서 Firebase - .catch (오류)로 클라이언트에 응답을 반환 할 수 없음

사용자 (admin) 인증을 위해 익스프레스 웹 서버에 firebase를 사용하고 있습니다. 기본적으로 모바일 앱과 별도로 관리자를위한 별도의 제어판이 있으므로 사용자의 데이터를 제어 할 수 있습니다.

나는 현재 다음과 같은 작업을 수행이 게시물 주소가 있습니다

app.post('/createNewAdminAccount', function(req, res){ 
    const password = bcryptPassword(bcryptDetails, req.body.password); 
    const username = req.body.username; 
    const auth = firebase.auth(); 
    let userExists = false; 

    function userExistsResponse(){ 
    res.setHeader('Content-Type', 'application/json'); 
    res.send(JSON.stringify({ "userExists": true })); 
    } 

    const promise = auth.createUserWithEmailAndPassword(username, String(password)); 
    promise.catch(function(error){ 
    console.log(error.message); 
    userExistsResponse(); 
    }); 
}); 

내가 userExistsResponse();이 방법은 정확히 내가 얼마나 아니다, 그러나 명확하게하기 위해, 그 같다.

.catch() 함수는 console.log()만을 허용하는 것으로 보입니다. 심지어 스태킹을 제안하는 this 보았다. 그러나 그것을 쓰는 아무리 작동하지 및 catch(error) 중 하나를 사용할 것으로 보이지 않습니다.

목표는 사용자가 인증되었고 메시지와 함께 클라이언트에 응답하는 것을 보는 것입니다. 난 (let userExists = false 변수가 나머지로 볼 수 있듯이), .catch()없이 .then() 문을 사용하여 함수를 호출하여 (서버가 오류로 인해 중단되게 함) 등의 작업을 시도했지만 아무 것도 작동하지 않습니다. YouTube 비디오, Firebase 문서 및 StackOverflow를 살펴본 후에 유용한 답을 찾지 못했습니다.

FIXED제공 : 내가 그것을 보는 방식에서 0x17을

app.post('/createNewAdminAccount', authenticated, function(req, res){ 
    const password = String(bcryptPassword(bcryptDetails, req.body.password)); 
    const username = req.body.username; 

    admin.auth().getUserByEmail(username) 
    .then(function(userRecord) { 
     res.setHeader('Content-Type', 'application/json'); 
     res.send(JSON.stringify({ 'userExists': true })); 
     console.log('User Exists'); 
    }) 
    .catch(function(error) { 
     const promise = auth.createUserWithEmailAndPassword(username, password); 
     promise.catch(function(error){ 
     console.log(error.message); 
     }); 
     console.log('User Created'); 
    }); 
}); 

답변

0

createNewAdminAccount 경로에 대한, 당신은 admin.auth().getUserByEmail()를 사용하여 첫 번째 장소에서 사용자에 대한 확인을하지 왜; 응답 코드 auth/email-already-exists을 분석하거나이 중에 나열된 일부 조합을 분석 한 다음 admin.auth().createUser()을 계속 진행 하시겠습니까?

또한 목표가 인 경우은 사용자를 인증합니다. 아래에서 언급 한 방법을 사용하지 말고 동일한 오류 코드를 분석해야합니까?

firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) { 
    // Handle Errors here. 
    var errorCode = error.code; 
    var errorMessage = error.message; 
    // ... 
}); 
+0

완벽하게 작동했습니다. 감사합니다. 나는'.res' 함수를'.then()'에 넣고'auth.create ... '를'.catch()'에러에 넣는다. –