Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

226
Views
Adding object to nested array using firebase, nextjs

I'm using nextjs, firebase to build my project, the project idea is a hybrid project management, the user is able to create a project with many boards and save tasks inside it: UI of the board

I want to save the board in this way: screen shot from Firebase

The code:

Here is how board array and project states looks:

    const [projectFields, setProjectFields] = useState({
    title: '',
    details: '',
    date: '',
});


    const [boardFields, setboardFields] = useState([
    {
        title: '',
        type: '',
        columns: [
            { name: "backlog" },
            { name: "In progress" },
            { name: "In review" }
        ]

    },
]);

sendProject function:

const sendProject = async () => {


    if (loading) return;
    setLoading(true);
    const docRef = await addDoc(collection(db, "projects"), {
        id: session.user.uid,
        projectDetails: projectFields,
        boards: boardFields,
        timestamp: serverTimestamp(),
    });


    setLoading(false);
    setProjectFields({
        title: '',
        details: '',
        date: '',
    })
    setboardFields({
        title: '',
        type: '',
        columns: [{}]

    })


};

sendTask function, which is update the "boards" in the doc in firebase wrongly:

    const sendTask = async () => {

    if (loading) return;
    setLoading(true);

    let object = {
        id: '1',
        priority: tasksFields.priority,
        title: tasksFields.title,
        date: tasksFields.date,
        details: tasksFields.details,
        chat: '0',
        attachment: '0'
    }


    const taskRef = doc(db, "projects", projectId ?? "1");
    const newBoards = [...project.boards];


    newBoards[boardIndex] = {

        ...newBoards[boardIndex],
        columns: {
            ...newBoards[boardIndex].columns[columnIndex],
            tasks: [...newBoards[boardIndex].columns[columnIndex].tasks, object]
        },

    }

    await updateDoc(taskRef, {

        ...project,
        boards: newBoards,

    });

    
    setLoading(false);
    setTasksFields(
        {
            id: '',
            priority: '',
            title: '',
            details: '',
            date: '',
            attachment: '0',
        }
    )


};

here is the result after updating the doc: wrong database schema picture

I don't know how to update it correctly, any help please? thank you all

about 4 years ago ยท Juan Pablo Isaza
1 answers
Answer question

0

I found the solution ๐Ÿ‘‡:

Updated sendTask function:

    const sendTask = async () => {


    if (loading) return;
    setLoading(true);

    let object = {
        id: '1',
        priority: tasksFields.priority,
        title: tasksFields.title,
        date: tasksFields.date,
        details: tasksFields.details,
        chat: '0',
        attachment: '0'
    }



    const taskRef = doc(db, "projects", projectId ?? "1");
    
    const newBoard = {...project.boards};
    const newColumn = [...project.boards[boardIndex].columns];


    newColumn[columnIndex] = {
            ...newColumn[columnIndex],
            tasks: [...newColumn[columnIndex].tasks, object]
    
    };

    newBoard[boardIndex] = {...newBoard[boardIndex], columns: newColumn};



    await updateDoc(taskRef, {

        ...project,
        boards: newBoard,

    });


    setLoading(false);
    setTasksFields(
        {
            id: '',
            priority: '',
            title: '',
            details: '',
            date: '',
            attachment: '0',
        }
    )

};




};
about 4 years ago ยท Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
ยฉ 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!