How to use algolia with firstore flutter? - firebase

I am trying to deploy some cloud functions using algolia search but it gives me some errors.
When deleting this line:
const algoliasearch = require('algoliasearch') and functions that related to algolia, it deploys successfully.
The error is:
=== Deploying to 'ecommerce-8e525'...
i deploying functions
+ functions: Finished running predeploy script.
i functions: ensuring required API cloudfunctions.googleapis.com is enabled...
i functions: ensuring required API cloudbuild.googleapis.com is enabled...
+ functions: required API cloudbuild.googleapis.com is enabled
+ functions: required API cloudfunctions.googleapis.com is enabled
i functions: preparing functions directory for uploading...
i functions: packaged functions (2.92 KB) for uploading
+ functions: functions folder uploaded successfully
The following functions are found in your project but do not exist in your local source code:
onCreateSection(us-central1)
onDeleteCategory(us-central1)
onDeleteSection(us-central1)
onUpdateCategory(us-central1)
onUpdateSection(us-central1)
If you are renaming a function or changing its region, it is recommended that you create the new function first before deleting the old one to prevent event loss.
For more info, visit https://firebase.google.com/docs/functions/manage-functions#modify
? Would you like to proceed with deletion? Selecting no will continue the rest of the deployments. No
i functions: creating Node.js 14 function onCreateSubCategory(us-central1)...
i functions: creating Node.js 14 function onUpdateSubCategory(us-central1)...
i functions: creating Node.js 14 function onDeleteSubCategory(us-central1)...
i functions: creating Node.js 14 function onUpdateMainCategory(us-central1)...
i functions: creating Node.js 14 function onDeleteMainCategory(us-central1)...
i functions: updating Node.js 14 function onDeleteProduct(us-central1)...
i functions: updating Node.js 14 function onCreateCustomer(us-central1)...
i functions: updating Node.js 14 function onUpdateCustomer(us-central1)...
i functions: updating Node.js 14 function onDeleteCustomer(us-central1)...
i functions: cleaning up build files...
Functions deploy had errors with the following functions:
onCreateSubCategory(us-central1)
onDeleteCustomer(us-central1)
onCreateCustomer(us-central1)
onUpdateSubCategory(us-central1)
onUpdateCustomer(us-central1)
onUpdateMainCategory(us-central1)
onDeleteProduct(us-central1)
onDeleteSubCategory(us-central1)
onDeleteMainCategory(us-central1)
To try redeploying those functions, run:
firebase deploy --only "functions:onCreateSubCategory,functions:onDeleteCustomer,functions:onCreateCustomer,functions:onUpdateSubCategory,functions:onUpdateCustomer,functions:onUpd
ateMainCategory,functions:onDeleteProduct,functions:onDeleteSubCategory,functions:onDeleteMainCategory"
To continue deploying other features (such as database), run:
firebase deploy --except functions
Error: Functions did not deploy properly.
This is my code:
const functions = require("firebase-functions");
const admin = require('firebase-admin');
const algoliasearch = require('algoliasearch');
const ALGOLIA_APP_ID = "APP_ID";
const ALGOLIA_ADMIN_KEY = "ADMIN_KEY";
const ALGOLIA_INDEX_NAME = "users";
admin.initializeApp();
const storage = admin.storage();
// When admin create sub category:
exports.onCreateSubCategory = functions.firestore
.document("/subCategories/{subCategoryId}")
.onCreate(async (snapshot, context) => {
const createdSubCategory = snapshot.data();
const subCategoryId = context.params.subCategoryId;
const mainCategoryId = createdSubCategory['main_category_id'];
// add it to main category
admin
.firestore()
.collection("mainCategories")
.doc(mainCategoryId).update({
'sub_categories_ids': admin.firestore.FieldValue.arrayUnion(subCategoryId)
});
});
// When admin update sub category:
exports.onUpdateSubCategory = functions.firestore.document("/subCategories/{subCategoryId}").onUpdate(async (change, context) => {
const updatedSubCategoryData = change.after.data();
const oldSubCategoryData = change.before.data();
const productsRef = admin.firestore().collection('products');
// case of updating image
// delete old image from storage
var oldImage = oldSubCategoryData['image_id'];
const bucket = storage.bucket();
const path = "subCategories/" + oldImage;
await bucket.file(path).delete();
// case of updating name
// update sub category name for related products
const productsQuerySnapshot = await productsRef.where('sub_category', '==', oldSubCategoryData['name']).get();
productsQuerySnapshot.forEach(doc => {
productsRef.doc(doc.id).update({
"sub_category": updatedSubCategoryData['name'],
"categories": admin.firestore.FieldValue.arrayRemove(oldSubCategoryData['name']),
"categories": admin.firestore.FieldValue.arrayUnion(updatedSubCategoryData['name']),
})
})
})
// When admin delete sub category:
exports.onDeleteSubCategory = functions.firestore.document("/subCategories/{subCategoryId}").onDelete(async (snapshot, context) => {
const subCategoryData = snapshot.data();
const imageId = subCategoryData['image_id'];
console.log('image_id is ',imageId);
// 1- delete sub category image from storage
const bucket = storage.bucket();
const path = "subCategories/" + imageId;
await bucket.file(path).delete();
// 2- delete sub category from products
const productsRef = admin.firestore().collection('products');
const productsQuerySnapshot = await productsRef.where('sub_category', '==', subCategoryData['name']).get();
productsQuerySnapshot.forEach( async doc => {
await productsRef.doc(doc.id).update({
'sub_category': {},
'categories': admin.firestore.FieldValue.arrayRemove(subCategoryData['name'])
})
})
})
//4) when admin update main category
exports.onUpdateMainCategory = functions.firestore.document("/mainCategories/{mainCategoryId}").onUpdate(async (change, context) => {
const updatedMainCategoryData = change.after.data();
const oldMainCategoryData = change.before.data();
const mainCategoryId = context.params.mainCategoryId;
//1- when admin change main category name
/// change main category name for related products
const productsRef = admin.firestore().collection('products');
const productsQuerySnapshot = await productsRef.where('categories', 'array-contains', oldMainCategoryData['name']).get();
productsQuerySnapshot.forEach( async doc =>{
await productsRef.doc(doc.id).update({
'categories': FieldValue.arrayRemove(oldMainCategoryData['name']),
'categories': admin.firestore.FieldValue.arrayUnion(updatedMainCategoryData['name'])
})
})
//2- when admin remove subCategory
/// delete them from subCategories collection
const oldSubCategoriesIds = oldMainCategoryData['sub_categories_ids'];
const updatedSubCategories = updatedMainCategoryData['sub_categories_ids'];
let difference = oldSubCategoriesIds.filter(x => !updatedSubCategories.includes(x));
difference.forEach(async id =>{
await admin.firestore().collection('subCategories').doc(id).delete();
})
})
// 5) when admin delete main category
exports.onDeleteMainCategory = functions.firestore.document("/mainCategories/{mainCategoryId}").onDelete(async (snapshot, context) => {
const mainCategoryData = snapshot.data();
const subCategoriesIds = mainCategoryData['sub_categories_ids'];
// delete category from products
const productsRef = admin.firestore().collection('products');
const productsQuerySnapshot = await productsRef.where('categories', 'array-contains', mainCategoryData['name']).get();
productsQuerySnapshot.forEach(async doc =>{
await productsRef.doc(doc.id).update({
'categories': admin.firestore.FieldValue.arrayRemove(mainCategoryData['name'])
})
})
// delete sub categories
subCategoriesIds.forEach(async id =>{
await admin.firestore().collection('subCategories').doc(id).delete();
})
})
// 6) when admin delete product
exports.onDeleteProduct = functions.firestore.document("/products/{productId}").onDelete(async (snapshot, context) => {
const categoryData = snapshot.data();
const imageId = categoryData['image_id'];
// delete it's image from storage
const bucket = storage.bucket();
const path = "products/" + imageId;
return bucket.file(path).delete();
})
// 7) onCreate Customer
exports.onCreateCustomer = functions.firestore
.document('users/{userId}')
.onCreate( async (snap, context) => {
const newValue = snap.data();
newValue.objectID = snap.id;
var client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.saveObject(newValue);
console.log("Finished");
});
// 8) onUpdate Customer
exports.onUpdateCustomer = functions.firestore
.document('users/{userId}')
.onUpdate( async (snap, context) => {
const afterUpdate = snap.after.data();
afterUpdate.objectID = snap.after.id;
var client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.saveObject(afterUpdate);
});
// 9) onDelete Customer
exports.onDeleteCustomer = functions.firestore
.document('users/{userId}')
.onDelete( async (snap, context) => {
const oldID = snap.id;
var client = algoliasearch(ALGOLIA_APP_ID, ALGOLIA_ADMIN_KEY);
var index = client.initIndex(ALGOLIA_INDEX_NAME);
index.deleteObject(oldID);
});
Edit I tried to run with "firebase deploy --only function --debug and this is the result "
Functions deploy had errors with the following functions: onCreateSubCategory(us-central1) onDeleteCustomer(us-central1) onDeleteProduct(us-central1) onCreateCustomer(us-central1) onDeleteMainCategory(us-central1) onUpdateMainCategory(us-central1) onUpdateCustomer(us-central1) onDeleteSubCategory(us-central1) onUpdateSubCategory(us-central1) To try redeploying those functions, run: firebase deploy --only "functions:onCreateSubCategory,functions:onDeleteCustomer,functions:onDeleteProduct,functions:onCreateCustomer,functions:onDeleteMainCategory,functions:onUpd ateMainCategory,functions:onUpdateCustomer,functions:onDeleteSubCategory,functions:onUpdateSubCategory" To try redeploying those functions, run: firebase deploy --only "functions:onCreateSubCategory,functions:onDeleteCustomer,functions:onDeleteProduct,functions:onCreateCustomer,functions:onDeleteMainCategory,functions:onUpdateMainCategory,functions: onUpdateCustomer,functions:onDeleteSubCategory,functions:onUpdateSubCategory"
To continue deploying other features (such as database), run: firebase deploy --except functions [2021-07-31T19:29:21.057Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onCreateSubCategory: firebase deploy --only "functions:onCreateSubCategory,functions:onDeleteCustomer,functions:onDeleteProduct,functions:onCreateCustomer,functions:onDeleteMainCategory,functions:onUpdateMainCategory,functions: onUpdateCustomer,functions:onDeleteSubCategory,functions:onUpdateSubCategory" To continue deploying other features (such as database), run: firebase deploy --except functions [2021-07-31T19:29:21.057Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onCreateSubCategory: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onDeleteCustomer: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onDeleteProduct: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onCreateCustomer: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onDeleteMainCategory: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onUpdateMainCategory: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onUpdateCustomer: firebase deploy --only "functions:onCreateSubCategory,functions:onDeleteCustomer,functions:onDeleteProduct,functions:onCreateCustomer,functions:onDeleteMainCategory,functions:onUpd ateMainCategory,functions:onUpdateCustomer,functions:onDeleteSubCategory,functions:onUpdateSubCategory"
To continue deploying other features (such as database), run: firebase deploy --except functions [2021-07-31T19:29:21.057Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onCreateSubCategory: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onDeleteCustomer: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onDeleteProduct: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onCreateCustomer: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onDeleteMainCategory: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onUpdateMainCategory: [2021-07-31T19:29:21.058Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onUpdateCustomer: [2021-07-31T19:29:21.059Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onDeleteSubCategory: [2021-07-31T19:29:21.059Z] Error during update for projects/ecommerce-8e525/locations/us-central1/functions/onUpdateSubCategory:
Error: Functions did not deploy properly.

Remove these installations and reinstall again solves the problem .
npm install -g firebase-tools
firebase login
firebase init
cd functions
npm install algoliasearch --save
firebase functions:config:set algolia.appid="YOUR_APP_ID" algolia.apikey="YOUR_API_KEY"

Related

update firebase upon cloud storage thumbnail completion

I have the following index.js that's triggered when a thumbnail is generated in cloud storage. It seems to work fine.
I'd like to replace the console.log line with code that adds a field like {"thumbnail_done":true} to the firebase document docid found in the script. I'm not clear on how to do that.
exports.thumbComplete = (event, context) => {
const fname = event.name;
let suffix = "_200x200.jpeg"
if (fname.endsWith(suffix)) {
let docid = fname.substring(0, fname.length - suffix.length);
console.log(`thumbnail processing complete: ${docid}`);
}
};
Thanks!
Got it working with the following:
// The Cloud Functions for Firebase SDK to create Cloud Functions and set up triggers.
const functions = require('firebase-functions');
// The Firebase Admin SDK to access Firestore.
const admin = require('firebase-admin');
admin.initializeApp();
exports.generateThumbnail = functions.storage.object().onFinalize(async (object) => {
const fname = object.name;
let suffix = "_200x200.jpeg"
if (fname.endsWith(suffix)) {
let docid = fname.substring(0, fname.length - suffix.length);
await admin.firestore().doc(`photo/${docid}`).update({ 'uploaded': true });
}
});

Firebase Cloud Function not executing Flutter

I have the following function in my index.ts file:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp(functions.config().firebase);
const db = admin.firestore();
const fcm = admin.messaging();
export const sendToDevice = functions.firestore
.document('orders/{orderId}')
.onCreate(async snapshot => {
print("aa")
console.log("osakosak");
const order = snapshot.data();
const querySnapshot = await db
.collection('users')
.doc(order.ustaID)
.collection('tokens')
.get();
const tokens = querySnapshot.docs.map(snap => snap.id);
const payload: admin.messaging.MessagingPayload = {
notification: {
title: 'New Order!',
body: `you sold a ${order.day} for ${order.time}`,
click_action: 'FLUTTER_NOTIFICATION_CLICK'
}
};
return fcm.sendToDevice(tokens, payload);
});
However, when the new document gets added into the order collection, this doesn't get triggered. Even the print and console.log don't work. I tried putting print and console log before export, and it still didn't fire.
Based on your comments ("It depends on cloud_firestore in pubspec.yaml"), it seems that you didn't deploy your Cloud Function correctly.
As a matter of fact, Cloud Functions are totally independent from your Flutter app (your front-end). It is a back-end service. You should deploy it with the Firebase CLI, see the doc. Note that the code shall be in the Firebase Project, not in your Flutter project.

How to do integration tests on Firebase Functions using local emulator?

I successfully wrote some tests for my Firebase functions, however now I want to test functions that manipulate Firestore data.
to do so I execute the following
export GOOGLE_APPLICATION_CREDENTIALS="my-project-key.json"
export FIRESTORE_EMULATOR_HOST="localhost:8080"
firebase emulators:start --import ./functions/test/fixture --project my-project
and then I run
npm run test
The test code is as follows:
const test = require("firebase-functions-test")()
const functions = require("../index")
describe("Tests", () => {
it("Do test", async () => {
const wrapped = test.wrap(functions.doTest)
const result = await wrapped({id:"1"})
})
})
The index file imported contains:
const functions = require("firebase-functions")
const admin = require("firebase-admin")
admin.initializeApp()
exports.doTest = functions.https.onCall(async (data) => {
const {id} = data
const vehicleRef = admin.firestore().collection("vehicles").doc(id)
const vehicle = await vehicleRef.get()
})
Yet every time I call a Firebase function that accesses Firestore I get the following error (in this case on vehicleRef.get()):
Error: Could not load the default credentials. Browse to https://cloud.google.com/docs/authentication/getting-started for more information.
at GoogleAuth.getApplicationDefaultAsync (/MyProject/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:157:19)
at processTicksAndRejections (internal/process/task_queues.js:94:5)
at async GoogleAuth.getClient (/MyProject/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:490:17)
at async GrpcClient._getCredentials (/MyProject/firebase/functions/node_modules/google-gax/build/src/grpc.js:87:24)
at async GrpcClient.createStub (/MyProject/firebase/functions/node_modules/google-gax/build/src/grpc.js:212:23)
Caused by: Error
at Firestore.getAll (/MyProject/firebase/functions/node_modules/#google-cloud/firestore/build/src/index.js:784:23)
at DocumentReference.get (/MyProject/firebase/functions/node_modules/#google-cloud/firestore/build/src/reference.js:201:32)
at Function.run (/MyProject/firebase/functions/src/tracker.js:40:28)
at wrapped (/MyProject/firebase/functions/node_modules/firebase-functions-test/lib/main.js:72:30)
at Context.<anonymous> (/MyProject/firebase/functions/test/testTracker.js:42:26)
at callFn (/MyProject/firebase/functions/node_modules/mocha/lib/runnable.js:366:21)
at Test.Runnable.run (/MyProject/firebase/functions/node_modules/mocha/lib/runnable.js:354:5)
at Runner.runTest (/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:677:10)
at /MyProject/firebase/functions/node_modules/mocha/lib/runner.js:801:12
at next (/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:594:14)
at /MyProject/firebase/functions/node_modules/mocha/lib/runner.js:604:7
at next (/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:486:14)
at Immediate._onImmediate (/MyProject/firebase/functions/node_modules/mocha/lib/runner.js:572:5)
What am I doing wrong?
Looks like you're doing everything right, except I don't think you're initializing the initializeApp object correctly. Try this in your index:
admin.initializeApp({
projectId: 'my-project',
credential: admin.credential.applicationDefault(),
});
You'll have to add a condition to use this version based on whether or not the environment variable is set though.

firebase deploy error in firebase cloud messaging

I integrate the chat application. when I log in I add fcm token in the firebase database and when sender and receiver chat I got chat notification on both sides.so I did below step
install node.js
I use node version (v13.11.0) and npm version (6.13.7)
then I use npm install -g firebase-tools (firebase-tools#7.16.1 updated 1 package in 18.481s)
firebase login (Already logged in as)
create a folder and add path then execute firebase init cmd after select function then add my project, select language, install npm ( Firebase initialization complete!)
I add below code in index.js
'use strict'
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.sendNotification = functions.database.ref('/Notifications/{receiver_id}/{notification_id}')
.onWrite((data, context) =>
{
const receiver_user_id = context.params.receiver_user_id;
const notification_id = context.params.notification_id;
console.log('We have a notification to send to :' , receiver_user_id);
if (!data.after.val())
{
console.log('A notification has been deleted :' , notification_id);
return null;
}
const DeviceToken = admin.database().ref(`/Users/${receiver_user_id}/device_token`).once('value');
return DeviceToken.then(result =>
{
const token_id = result.val();
const payload =
{
notification:
{
title: "New Chat Request",
body: `you have a new Chat Request, Please Check.`,
icon: "default"
}
};
return admin.messaging().sendToDevice(token_id, payload)
.then(response =>
{
console.log('This was a notification feature.');
});
});
});
execute Firebase deploy command I got below error
=== Deploying to 'notification-ad12a8'...
i deploying functions
i functions: ensuring necessary APIs are enabled...
+ functions: all necessary APIs are enabled
i functions: preparing functions directory for uploading...
**Error: An unexpected error has occurred.**
I tried 3 -4 times but not got any solution.please check code please help me..thank you

TypeError: functions.database is not a function

I want to update or creat an object, but i have this error :"TypeError: functions.database is not a function" on the registry of firebase function
this is my code:
const functions = require('firebase-functions');
exports.actualizar = functions.https.onRequest((request, response) => {
const obj = request.body;
const MAC = obj.MAC;
functions.database().ref ('/sensores/{MAC}').update(obj).promise.then(() =>
{
console.log("UpDate Success");
return req.status(200).send("ok");
})
.catch(() => {
functions.database.ref('/sensores'). set(obj).promise.then(() =>{
console.log ("Created Succces");
return req.status(200).send("");
})
.catch(() =>{
console.log("Error");
return req.status(500).send("error");
})
})
});
You can't use the Cloud Functions for Firebase SDK to query the database. It's just used for building the function definition. To query your database or other Firebase products, you need to use the Firebase Admin SDK, or whatever SDK is normally used to do so.
For example, you will see lots of official sample code that starts like this:
const admin = require('firebase-admin'); // this is the Admin SDK, not firebase-functions
admin.initializeApp();
// Then use "admin" to reach into Realtime Database, Firestore, Cloud Storage, etc.

Resources