如何使用第一层颤振的algolia ?



我试图使用algolia搜索部署一些云功能,但它给了我一些错误。删除这一行时:const algoliasearch = require('algoliasearch')和与algolia相关的功能,它成功部署。

错误是:

=== 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.

这是我的代码:

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);
});

编辑我试图运行"firebase部署-only功能-调试,这是结果">

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.

删除这些安装并重新安装即可解决问题。

  1. npm install -g firebase-tools
  2. firebase login
  3. firebase init
  4. cd functions
  5. npm install algoliasearch --save
  6. firebase functions:config:set algolia.appid="YOUR_APP_ID" algolia.apikey="YOUR_API_KEY"

相关内容

  • 没有找到相关文章

最新更新