サンプル2(POSTサンプル)
概要
Firebase Functionsのコード
機能
コード例
解説
アプリ側のコード
機能
コード例
解説
最終更新
最終更新
const post_data = functions.https.onCall(async (data, context) => {
// ユーザー認証
const username = data.header.username;
const password = data.header.password;
if (username != 'admin' || password != 'AA00XX11') {
throw new functions.https.HttpsError('unauthenticated', 'The function must be called while authenticated.');
}
// データの保存
const body = data.body;
return admin.firestore().collection('Item').add(body)
.then(docRef => {
return { message: `Document added with ID: ${docRef.id}` };
})
.catch(error => {
throw new functions.https.HttpsError('internal', `Error adding document: ${error}`);
});
});
exports.post_data = post_data;
const post_data = httpsCallable(functions, 'post_data');
post_data({ 'header' : {
'username': 'admin',
'password': 'AA00XX11'
},
'body' : {
'name': 'ぶどう'
}
}).then((result) => {
log(result.data);
}).catch((error) => {
log(error);
});