Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

reactjs - Firestore promise chaining 2nd chain onsnapshot cant find document

react,redux,es6, firestore onsnapshot quering I want to update a set of questions based on when a blog changes which itself changes when a category changes. Ive managed to set the blog with firestore querying. however when I try to chain onSnapshot with promises, the 2nd onSnapshot fails. Im calling the 2nd onsnapshot together with update as the 2nd chain with promise all.

what isnt clear to me is why firestore doesnt find the document in the 2nd onSnapshot.

increment method is based on the firebase increment method:

firebase.firestore.FieldValue.increment()

every help is welcomed, thanks in advance

my code:

all inside a redux toolkit thunk action:

export const getMainBlog = category => dispatch => {
...
}

setting paths for firestore to be used later:

const blogInCategoryCounter = firebase.db.collection(`categories/${category}/local-blogs`);
const getBlog = blogInCategoryCounter.orderBy("counterShownAllUsers","desc").limit(1)
const blogQuestionIndex = 
firebase.db.collection("categories").doc(category).collection("blog-question-index");

first promise returned from function in the chain:

const getDoc = () => {          
          let promise = new Promise((resolve,reject) => {           
              getBlog.onSnapshot(snapshot => {
                const data = snapshot.docs.map(doc => {                
                 if (doc.exists){                 
                 return ({
                   id: doc.id,
                   ...doc.data()
                 })                              
                 }else{   
                  console.log("blog not found");                
                   return null;
                 }          
               })
               const [dataExtracted] = data
               resolve(dataExtracted)
              })                             
            })
              return promise;
            }

2nd chain of promises including an update method and another onSnapshot:

    const updateCounterBlog = data => {
                  let promise = new Promise((resolve,reject) => {
                    const returnedFromUpdate = blogInCategoryCounter.doc(data.id).update({
                      counterShownAllUsers: firebase.increment(1)
                    })
                    returnedFromUpdate.then(() => resolve(data))
                  })
                  return promise  
                }
                
                const extractQuestionIndexQuestions = data => {
                  let promise = new Promise((resolve,reject) => {                                 
                      blogQuestionIndex.doc(data.id).onSnapshot(doc => {                                                                            
                          if (doc.exists){                                                                                                                       
                             resolve(doc.data())                                                                  
                          }else{
                             reject("index doc not found")                   
                          }
                        })                                                     
                  })
                  return promise;
                } 

array of promises:

        const promiseArray = [extractQuestionIndexQuestions,updateCounterBlog]

invocation:

getDoc().then(data => {
            Promise.all(promiseArray.map(callback => callback(data))).then(values => {              
               const blog = {
                      blogData: data,
                      blogId: data.id,
                      category: category,
                      blogQuestionIndex: values[0]
                    }                                                
              dispatch(loadBlog(blog)) // for redux thunk
            })                         
 })
question from:https://stackoverflow.com/questions/65952661/firestore-promise-chaining-2nd-chain-onsnapshot-cant-find-document

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ive got it to work, Now Im pretty sure it was a space typo in the data entry on the server.

sorry for the trouble if anyone took a look and tried to help, thanks if you did!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...