Skip to content

Instantly share code, notes, and snippets.

@shinriyo
Created July 24, 2020 09:39
Show Gist options
  • Save shinriyo/fdd9285b7a002dc3697b9d29c6e28d4f to your computer and use it in GitHub Desktop.
Save shinriyo/fdd9285b7a002dc3697b9d29c6e28d4f to your computer and use it in GitHub Desktop.
Sync Document for update (AddSearch with Firestore, Functions, TypeScript)
import { index, baseURL, siteKey, secretKey } from './index';
import * as functions from 'firebase-functions'
export const userOnUpdateForAddSearch = functions.firestore
.document('users/{userId}')
.onUpdate(async (event, context) => {
const axios = require('axios');
// const oldData = event.before.data();
const newData = event.after.data();
const documentId = event.after.id;
const args = {
withCredentials: true,
data: { ...newData },
headers: {
"Content-Type": "application/json",
}
}
// PUT /v2/indices/{index public key}/documents/{document id}
axios.put(`${baseURL}/v2/indices/${siteKey}/documents/${documentId}`, args, {
// HTTP Basic Auth
auth: {
username: siteKey,
password: secretKey,
}
})
.then((response: any) => {
console.log(response.data)
})
.catch((error: any) => {
console.log(error)
})
.then(function () {
console.log("*** finish ***")
})
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment