Firebase Firestore 不會保留 array



我有一个云函数,它处理和存储发送给它的数据。除了一些ID值外,大多数数据都来自请求,我存储在正在使用的主集合中以供参考。我的问题是,我从另一个云消防商店集合中获得的数据没有保存在我正在编写的集合中。我的代码如下:

await emails.forEach(async (email: string) => {
try {
// This should only ever return one result
const student = await usersRef.where('userEmail', '==', email).get();
if (student.empty) {
console.log(`No matching documents for email: ${email}`);
} else {
student.forEach((s) => {
const studentData = s.data();
console.log('found student: ', studentData);
// StudentIds stores reference to student objects
studentIds.push(studentData.playerUniqueID); 
});
}
} catch (err) {
console.log('Error finding students: ', err);
throw new functions.https.HttpsError('internal', 'Error finding students');
}
});
const sessionId = uuidv4();
const newSession: newSessionWriteRequest = {
emails, // emails is also an array of strings and is persisted properly
owner,
// StudentIds is not empty at this point and is populated correctly. StudentIds is an array of strings
studentIds,
ongoing: true,
sessionName: data.sessionName,
startTime: data.sessionStartDate,
sessionId
};
try {
// All values except studentIds are persisted to the sessionsRef except studentIds, which is a blank array
await sessionsRef.doc(sessionId).set(newSession);
console.log('new session: ', newSession);
return newSession;
} catch (err) {
console.log('Error creating new session: ', err);
throw new functions.https.HttpsError('internal', 'Error creating new session');
}

StudentIds只是一个字符串数组。CCD_ 2也是一个字符串数组,但存储正确。两者之间唯一的区别是emails来自对函数的初始请求,而studentIds来自firestore。我的问题是为什么studentIds没有被正确地持久化?我不知道火球收集之间是否存在某种互动?

问题在于这段代码:

await emails.forEach(async (email: string) => {
try {
// This should only ever return one result
const student = await usersRef.where('userEmail', '==', email).get();
if (student.empty) {
console.log(`No matching documents for email: ${email}`);
} else {
student.forEach((s) => {
const studentData = s.data();
console.log('found student: ', studentData);
// StudentIds stores reference to student objects
studentIds.push(studentData.playerUniqueID); 
});
}
} catch (err) {
console.log('Error finding students: ', err);
throw new functions.https.HttpsError('internal', 'Error finding students');
}
});

正如评论中所指出的,我在forEach循环前面的await什么也没做。将其更改为这个解决了问题:

const studentIds: string[] = await getStudentPlayerUniqueIds(emails);
...
const getStudentPlayerUniqueIds = async(emails: string[]): Promise<string[]> => {
const studentIds: string[] = []
try {
for (const email of emails) {
const student = await usersRef.where('userEmail', '==', email).get();
if (student.empty) {
console.log(`No matching documents for email: ${email}`);
} else {
student.forEach((s) => {
const studentData = s.data();
console.log('found student: ', studentData);
studentIds.push(studentData.playerUniqueID);
});
}
}
return studentIds;
} catch (err) {
console.log('Error finding students: ', err);
throw new functions.https.HttpsError('internal', 'Error finding students');
}
}

相关内容

  • 没有找到相关文章

最新更新