Aug*_*ann 5 javascript firebase reactjs google-cloud-firestore
我正在使用 Firebase oAuth 构建应用程序。我遵循了所有说明,但我的代码返回了一个错误,指出“异步箭头函数预期没有返回值”。
我看到有多个标题相同的帖子,但没有找到答案。
import firebase from 'firebase/app';
import 'firebase/firestore';
import 'firebase/auth';
export const auth = firebase.auth();
export const firestore = firebase.firestore();
const config = {
apiKey: 'AIzaSyDptaU9-hQIIpAW60S1-aGUSdcQdijbNAQ',
authDomain: 'ecommerce-50be3.firebaseapp.com',
projectId: 'ecommerce-50be3',
storageBucket: 'ecommerce-50be3.appspot.com',
messagingSenderId: '948534790840',
appId: '1:948534790840:web:6329c3edcc717138a5424d',
};
firebase.initializeApp(config);
export const createUserProfileDocument = async (userAuth, additionalData) => {
if (!userAuth) return;
const userRef = firestore.doc(`users/${userAuth.uid}`);
const snapShot = await userRef.get();
if (!snapShot.exists) {
const { displayName, email } = userAuth;
const createdAt = new Date();
try {
await userRef.set({
displayName,
email,
createdAt,
...additionalData,
});
} catch (error) {
console.log('error creating user', error.message);
}
}
return userRef;
};
const provider = new firebase.auth.GoogleAuthProvider();
provider.setCustomParameters({ prompt: 'select_account' });
export const signInWithGoogle = () => auth.signInWithPopup(provider);
export default firebase;
Run Code Online (Sandbox Code Playgroud)
将第一行从if (!userAuth) return
to更改为if (!userAuth) return {}
,这应该可以解决问题
解释
您遇到的错误consistent-return
在最后说,这基本上意味着您的函数应该始终返回相同的类型。
考虑到这一点,如果我们看一下您函数中的第一行
if (!userAuth) return;
Run Code Online (Sandbox Code Playgroud)
此行返回 a void
,但函数中的最后一行返回其他类型
return userRef;
Run Code Online (Sandbox Code Playgroud)
userRef
绝对不是类型void
,这就是您遇到此错误的原因。
该规则要求return
语句要么总是指定值,要么从不指定值。
此规则的错误代码示例:
function doSomething(condition) {
if (condition) {
return true;
} else {
return;
}
}
function doSomething(condition) {
if (condition) {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
此规则的正确代码示例:
function doSomething(condition) {
if (condition) {
return true;
} else {
return false;
}
}
// or even better:
function doSomething(condition) {
return condition;
}
Run Code Online (Sandbox Code Playgroud)
如果您想禁用此规则,可以使用此注释:
// eslint-disable-next-line consistent-return
return userRef;
Run Code Online (Sandbox Code Playgroud)