Cloud Firestore deep get with subcollection

后端 未结 6 1171
离开以前
离开以前 2020-12-07 18:34

Let\'s say we have a root collection named \'todos\'.

Every document in this collection has:

  1. title: String
  2. subcollection named
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 19:10

    If anyone is still interested in knowing how to do deep query in firestore, here's a version of cloud function getAllTodos that I've come up with, that returns all the 'todos' which has 'todo_items' subcollection.

    exports.getAllTodos = function (req, res) {
        getTodos().
            then((todos) => {
                console.log("All Todos " + todos) // All Todos with its todo_items sub collection.
                return res.json(todos);
            })
            .catch((err) => {
                console.log('Error getting documents', err);
                return res.status(500).json({ message: "Error getting the all Todos" + err });
            });
    }
    
    function getTodos(){
        var todosRef = db.collection('todos');
    
        return todosRef.get()
            .then((snapshot) => {
                let todos = [];
                return Promise.all(
                    snapshot.docs.map(doc => {  
                            let todo = {};                
                            todo.id = doc.id;
                            todo.todo = doc.data(); // will have 'todo.title'
                            var todoItemsPromise = getTodoItemsById(todo.id);
                            return todoItemsPromise.then((todoItems) => {                    
                                    todo.todo_items = todoItems;
                                    todos.push(todo);         
                                    return todos;                  
                                }) 
                    })
                )
                .then(todos => {
                    return todos.length > 0 ? todos[todos.length - 1] : [];
                })
    
            })
    }
    
    
    function getTodoItemsById(id){
        var todoItemsRef = db.collection('todos').doc(id).collection('todo_items');
        let todo_items = [];
        return todoItemsRef.get()
            .then(snapshot => {
                snapshot.forEach(item => {
                    let todo_item = {};
                    todo_item.id = item.id;
                    todo_item.todo_item = item.data(); // will have 'todo_item.title' and 'todo_item.completed'             
                    todo_items.push(todo_item);
                })
                return todo_items;
            })
    }
    

提交回复
热议问题