通过添加带有 "dynamic name" 的新字段来更新 Firestore 文档的语法 (React-Redux)

Syntax to update a Firestore document by adding a new field with a "dynamic name" (React-Redux)

我正在尝试通过添加新的“问题数组”(包含数组内部问题的详细信息)来更新 Firestore 文档。

我想为 firestore 文档中的这个新“字段”命名,以遵循一致的命名约定,其值随文档中问题数组的数量动态变化。

即在文档中添加第 5 个问题数组会将文档中的当前问题数 (4) 加 1 (使 5)。然后它将这个值附加到正在创建的新字段的实际名称上(即'question/+(5)/ = [question details]。下面的代码可能会阐明我要完成的事情。

export const createFlashcardQuestion = (state) => {

    return (dispatch, getState, { getFirebase, getFirestore}) => {

         (...)
    
        const numberOfQuestions = state.numberOfQuestions;    <--- THIS IS THE VALUE I WANT TO REPLACE THE # with

        # = numberOfQuestions;                                <----- Placed here for clarity that this is the value trying to be ammended to the question array name.

        firestore.collection(...PATH BLAH BLAH...).update({   <----- I SIMPLIFIED THE PATH JUST FOR THIS QUESTION
            numberOfQuestions: numberOfQuestions,             <---- THIS IS THE NEW # OF QUESTIONS
            lectureUpdatedAt: new Date(),
            question#: [state],                               <-------- THIS IS THE LINE I AM STRUGGLING. I want the "#" to somehow equal the interger value found in "numberOfQuestions".
            

        }).then(() => {

            (...Dispatch and Thunk Mumbo Jumbo...)

    
};

找到解决方案:

所以经过进一步调查,我找到的解决这个问题的唯一方法是调整调用的内容。与其尝试动态命名该字段,不如将“键”作为对象数组中对象之一的名称传递(对象数组名为“问题”)。对象数组,每个索引包含包含问题详细信息的对象“state”。

此解决方案需要对文档进行 2 次更新; 1 将新问题对象添加到文档中,另一个更新是对文档中预先存在的字段进行更改。

export const createFlashcardQuestion = (state) => {

    return (dispatch, getState, { getFirebase, getFirestore}) => {

         (...)
    

        const key = state.numberOfQuestions;

        var newQuestion = {};
        newQuestion[`questions.${key}`] = {
            state
        };

        firestore.collection(...PATH BLAH BLAH...).update(
            newQuestion)

        firestore.collection(...PATH BLAH BLAH...).update({
            numberOfQuestions: numberOfQuestions,
            lectureUpdatedAt: new Date(),
            

        }).then(() => {

            (...Dispatch and Thunk Mumbo Jumbo...)

    
};
``