世博会项目中的 Firebase Cloud 功能
Firebase Cloud function in expo project
所以我有一个云功能(这还不在反应本机应用程序目录中):
const admin = require('firebase-admin');
const firebase_tools = require('firebase-tools');
const functions = require('firebase-functions');
admin.initializeApp();
exports.deleteUser = functions
.runWith({
timeoutSeconds: 540,
memory: '2GB'
})
.https.onCall((data, context) => {
const userId = context.auth.uid;
var promises = [];
// DELETE DATA
var paths = ['users/' + userId, 'messages/' + userId, 'chat/' + userId];
paths.forEach((path) => {
promises.push(
recursiveDelete(path).then( () => {
return 'success';
}
).catch( (error) => {
console.log('Error deleting user data: ', error);
})
);
});
// DELETE FILES
const bucket = admin.storage().bucket();
var image_paths = ["avatar/" + userId, "avatar2/" + userId, "avatar3/" + userId];
image_paths.forEach((path) => {
promises.push(
bucket.file(path).delete().then( () => {
return 'success';
}
).catch( (error) => {
console.log('Error deleting user data: ', error);
})
);
});
// DELETE USER
promises.push(
admin.auth().deleteUser(userId)
.then( () => {
console.log('Successfully deleted user');
return true;
})
.catch((error) => {
console.log('Error deleting user:', error);
})
);
return Promise.all(promises).then(() => {
return true;
}).catch(er => {
console.error('...', er);
});
});
function recursiveDelete(path, context) {
return firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token
})
.then(() => {
return {
path: path
}
}).catch( (error) => {
console.log('error: ', error);
return error;
});
}
// [END recursive_delete_function]
这用于我的 swift 应用程序。我如何将它用于我用 Expo 构建的 React 本机应用程序?
我安装了以下yarn add @react-native-firebase/functions
我在根目录中设置了 firebase.js 文件:
import * as firebase from "firebase";
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "test",
authDomain: "test",
databaseURL: "test",
projectId: "test",
storageBucket: "test",
messagingSenderId: "test",
appId: "test"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
export default firebase;
我有一个按钮:
<Text>Delete Account</Text>
<View>
<Button
title="Delete Account"
color="#F9578E"
accessibilityLabel="Delete Account"
/>
</View>
点击后用户退出并运行上述云功能。
我不熟悉react-native和Expo,但是从@react-native-firebase/functions
documentation看来,你需要做如下:
import functions from '@react-native-firebase/functions';
function App() {
useEffect(() => {
functions()
.httpsCallable('deleteUser')()
.then(response => {
// ....
});
}, []);
// ...
}
您没有将任何数据从您的应用传递到您的 Callable Cloud Function,即您没有在您的 Cloud Function 中使用 data
对象,这就是您需要 functions().httpsCallable('deleteUser')()
的原因。
如果您需要传递一些数据,文档显示了一个示例 here,传递一个对象:
functions().httpsCallable('listProducts')({
page: 1,
limit: 15,
})
(这完全符合 Firebase JS SDK 调用 Callable Cloud Function 的方式,这就是我回答这个问题的原因,即使我对 react-native 缺乏了解在世博会...)
所以我有一个云功能(这还不在反应本机应用程序目录中):
const admin = require('firebase-admin');
const firebase_tools = require('firebase-tools');
const functions = require('firebase-functions');
admin.initializeApp();
exports.deleteUser = functions
.runWith({
timeoutSeconds: 540,
memory: '2GB'
})
.https.onCall((data, context) => {
const userId = context.auth.uid;
var promises = [];
// DELETE DATA
var paths = ['users/' + userId, 'messages/' + userId, 'chat/' + userId];
paths.forEach((path) => {
promises.push(
recursiveDelete(path).then( () => {
return 'success';
}
).catch( (error) => {
console.log('Error deleting user data: ', error);
})
);
});
// DELETE FILES
const bucket = admin.storage().bucket();
var image_paths = ["avatar/" + userId, "avatar2/" + userId, "avatar3/" + userId];
image_paths.forEach((path) => {
promises.push(
bucket.file(path).delete().then( () => {
return 'success';
}
).catch( (error) => {
console.log('Error deleting user data: ', error);
})
);
});
// DELETE USER
promises.push(
admin.auth().deleteUser(userId)
.then( () => {
console.log('Successfully deleted user');
return true;
})
.catch((error) => {
console.log('Error deleting user:', error);
})
);
return Promise.all(promises).then(() => {
return true;
}).catch(er => {
console.error('...', er);
});
});
function recursiveDelete(path, context) {
return firebase_tools.firestore
.delete(path, {
project: process.env.GCLOUD_PROJECT,
recursive: true,
yes: true,
token: functions.config().fb.token
})
.then(() => {
return {
path: path
}
}).catch( (error) => {
console.log('error: ', error);
return error;
});
}
// [END recursive_delete_function]
这用于我的 swift 应用程序。我如何将它用于我用 Expo 构建的 React 本机应用程序?
我安装了以下yarn add @react-native-firebase/functions
我在根目录中设置了 firebase.js 文件:
import * as firebase from "firebase";
// Your web app's Firebase configuration
var firebaseConfig = {
apiKey: "test",
authDomain: "test",
databaseURL: "test",
projectId: "test",
storageBucket: "test",
messagingSenderId: "test",
appId: "test"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
export default firebase;
我有一个按钮:
<Text>Delete Account</Text>
<View>
<Button
title="Delete Account"
color="#F9578E"
accessibilityLabel="Delete Account"
/>
</View>
点击后用户退出并运行上述云功能。
我不熟悉react-native和Expo,但是从@react-native-firebase/functions
documentation看来,你需要做如下:
import functions from '@react-native-firebase/functions';
function App() {
useEffect(() => {
functions()
.httpsCallable('deleteUser')()
.then(response => {
// ....
});
}, []);
// ...
}
您没有将任何数据从您的应用传递到您的 Callable Cloud Function,即您没有在您的 Cloud Function 中使用 data
对象,这就是您需要 functions().httpsCallable('deleteUser')()
的原因。
如果您需要传递一些数据,文档显示了一个示例 here,传递一个对象:
functions().httpsCallable('listProducts')({
page: 1,
limit: 15,
})
(这完全符合 Firebase JS SDK 调用 Callable Cloud Function 的方式,这就是我回答这个问题的原因,即使我对 react-native 缺乏了解在世博会...)