1
// The Cloud Functions for Firebase SDK to create Cloud Functions and 
setup triggers. 
const functions = require('firebase-functions'); 

// The Firebase Admin SDK to access the Firebase Realtime Database. 
const admin = require('firebase-admin'); 
admin.initializeApp(functions.config().firebase); 

exports.giveCard = functions.firestore 
.document('Profiles/{profileId}/cards/{cardsId}/_given/{_givenID}') 
.onWrite((event) => { 

    // Get the field values of what I am working with 
    const oldGiven = event.data.previous.data()['given']; 
    const newGiven = event.data.data()['given']; 

    // Get the cardID to make sure that is there 
    const cardID = event.params.cardsId; 

    // An array to go through 
    const give_profiles = event.data.data()['given_profiles']; 

    // error cardDatatwo is returned as undefined 
    const cardDatatwo = newGiven.parent; 

    // error cardDatathree is returned as undefined 
    const cardDatathree = event.data.ref.root 

    // // error cardDatafour cannot read propoerty of undefined 
    // const cardDatafour = cardDatathree.child('Profiles/{profileId}/cards/{cardsId}') 

    // error cardDatafive 'The value of cardfive is DocumentReference... 
    const cardDatafive = event.data.ref.firestore.doc('Profiles/{profileId}/cards/{cardsId}'); 

    // Check that the values have changed 
    if (newGiven == oldGiven) return; 

    if (newGiven !== undefined) { 

     console.log('The old value of given is', oldGiven); 
     console.log('The new value of given is', newGiven); 
     console.log('The value of the card is', cardID); 
     console.log('The value of the cardtwo is', cardDatatwo); 
     console.log('The value of the cardthree is', cardDatathree); 
     // console.log('The value of the cardfour is', cardDatafour); 
     console.log('The value of the cardfive is', cardDatafive); 

     for (var profile of give_profiles) { 
      console.log(profile); 
     }; 
     return; 
    } 
    return console.log("No given value"); 
}); 

Cloudstore가 클라우드 기능을 사용하여 루트 권한을 얻는 데 큰 어려움을 겪고 있습니다. 그것은 다르게 작동합니다.클라우드 기능 및 Firestore가 Firebase의 루트가되지 않습니다.

onUpdate가 더 내려간 후 루트 경로를 따라 값을 얻으려고합니다.

.parent 또한 노드 및 event.data.ref.firestore 작동하지 않습니다) 즉 실시간 데이터베이스 의대로 작동하지 않습니다 물론 functions.database.ref을 작동하지 않고 firebase.firestore을 (사용할 수 없습니다 .doc가 정의되지 않은 상태로 돌아옵니다.

나는 모든 옵션을 선택했음을 확신합니다.

희망 하시겠습니까?

+0

당신이 가지고있는 거리와 부모를 구현해야하는 위치를 보여주는 코드를 공유 할 수 있습니까? 이것이 DocumentSnapshot을 사용하는 청취자 콜백 내부에 있을까요? – Grimthorr

+0

코드를 보여주고 파이어 스토어 API를 사용하려면 firebase-admin 모듈을 최신 버전으로 업데이트해야한다는 것을 기억하십시오. 답장은 –

답변

1

우와 the documentation에 따르면,이 같은 firestore를 통해 컬렉션에 액세스 할 수 있습니다

exports.giveCard = functions.firestore 
.document('Profiles/{profileId}/cards/{cardsId}/_given/{_givenID}') 
.onWrite((event) => { 

    // other code 

    const ref = event.data.ref.firestore.doc('your/path/here'); 
    return ref.set({foo: 'bar'}).then(res => { 
     console.log('Document written'); 
    }); 

}); 
당신은 액세스 추구하고있는 데이터베이스의 어떤 부분에 대한 경로를 구축 firestore을 사용할 수 있습니다

.

exports.giveCard = functions.firestore 
.document('Profiles/{profileId}/cards/{cardsId}/_given/{_givenID}') 
.onWrite((event) => { 

    // other code 

    const parentref = event.data.ref.parent; 
    const grandparentref = parentref.parent; // gets to cardsId 
    return grandparentref.set({foo: 'bar'}).then(res => { 
     console.log('Document written'); 
    }); 

}); 
+0

입니다. 미안, 나도 그걸 넣어야 해. 전에 해봤 어. 아직 정의되지 않았습니다. 나는 이제 모든 가능한 순열을 시도했다고 확신한다. 오류 메시지와 함께 코드를 추가하도록 코드를 변경했습니다. – woisme

+0

나는 그것을 직접 시도했기 때문에 효과가있다. 그러나 참조'const cardDatafive = event.data.ref.firestore.doc ('Profiles/{profileId}/cards/{cardsId}') '는 중괄호로 인해 작동하지 않습니다. 데이터를 쓰거나 읽으려는 특정 경로에 와일드 카드를 사용할 수 없습니다. –

+0

필자는 필자가 작성한 경로의 상위 경로에 액세스하기 위해 작동하는'event.data.ref.parent'를 사용하여 예제를 보여주기 위해 내 대답을 업데이트했습니다. –

관련 문제