你好,我正在创建一个可调用的Firebase云函数,该函数将返回一个数组。但是它什么也不会返回,所以我检查了功能控制台并显示了一个错误:
Unhandled error TypeError: Cannot read property 'forEach' of undefined
at /user_code/lib/index.js:1352:24
at Array.forEach (native)
at Object.<anonymous> (/user_code/lib/index.js:1346:42)
at next (native)
at fulfilled (/user_code/lib/index.js:4:58)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
我让您键入强制转换所有数组,但仍然显示错误。这是该功能的代码
export const getRelatedItems = functions.https.onCall(async (data, context)=>{
const searchItemCategory:string = data.item_category;
const relateItemUids:string[] = [];
let uniqueWordArray:string[] = [];
let wordCountArray:number[] = [];
const relatedItemMap = [];
const user_uid = data.current_user_uid;
const userItemProfileQuery = await admin.firestore()
.doc('User_Item_Profile/'+user_uid+'/user_item_profile/'+searchItemCategory)
.get();
if (userItemProfileQuery.exists){
console.log('User has an existing User Item Profile');
const userItemProfileDoc = userItemProfileQuery.data();
uniqueWordArray = userItemProfileDoc['user_item_profile_attributes'];
wordCountArray = userItemProfileDoc['user_item_profile_count'];
}
console.log("Looking for items that belong to the "+searchItemCategory+" category");
//get items with the same item category
const querySnapshot = await admin.firestore().collection('Item_Profile')
.where("item_profile_item_category", "==", searchItemCategory).get();
const resultDocs = querySnapshot.docs;
console.log('There are '+resultDocs.length+' in the '+searchItemCategory);
const readPromise = await resultDocs.forEach(async function (doc) {
const itemProfile = doc.data();
const itemUid = itemProfile['item_profile_item_uid'];
const itemProfileAttributeWords:string[] = itemProfile['item_profile_attribute_words'];
const itemProfileAttributeWeight:number[] = itemProfile['item_profile_attribute_weights'];
let itemScore:number = 0;
uniqueWordArray.forEach(function (attributeWord) {
console.log(itemProfileAttributeWords);
console.log(attributeWord);
if (arrayContains(itemProfileAttributeWords, attributeWord)){
const indexOfAttributeWord = itemProfileAttributeWords.indexOf(attributeWord);
const indexOfUniqueWord = uniqueWordArray.indexOf(attributeWord);
const weight = itemProfileAttributeWeight[indexOfAttributeWord];
const wordCount = wordCountArray[indexOfUniqueWord];
console.log("The word "+attributeWord+" is in the query and has a score of "+(weight*wordCount));
itemScore+=(weight*wordCount);
}
});
console.log("Stored the item "+itemUid+" with a score of "+itemScore+" to the Map");
relatedItemMap.push([itemUid, itemScore]);
});
console.log("Started Sorting the Map");
//sort the map based on the score for each item in ascending order
const sortedArray = relatedItemMap.sort(function (a,b) {
return a[1]<b[1]? 1:a[1]>b[1]?-1:0;
});
console.log("Finished Sorting the Map");
sortedArray.forEach(function (item) {
relateItemUids.push(item[0]);
});
relateItemUids.forEach(function (item) {
console.log("Recommended Item UID: "+item);
});
console.log("Array of related item uids has been sent");
return {
itemUids: relateItemUids
}
});
过去三天我一直在尝试解决此问题,但似乎无法解决