programing

Firebase에서 배열 유형 문서 항목에 타임스탬프를 할당하는 방법

lovejava 2023. 6. 8. 19:06

Firebase에서 배열 유형 문서 항목에 타임스탬프를 할당하는 방법

Cloud Firestore에서 항목을 어레이로 동적으로 푸시할 수 있는 Vue.js 앱을 구축하려고 합니다.항목은 다음에 있습니다.events배열 - 다양한 개체를 포함하며, 그 중 하나가 타임스탬프입니다.아래 기능을 참조하십시오.

let newDetails = this.newDetails
let newImage = this.imageUrl
let timestamp = moment(Date.now()).format('lll')
let ref = db.collection('users').where('user_id', '==', firebase.auth().currentUser.uid)
    .get()
    .then(function (querySnapshot) {
        querySnapshot.forEach(function (doc) {
            console.log(doc.id, ' => ', doc.data())
            doc.ref.update({
                'events': firebase.firestore.FieldValue.arrayUnion({
                    'name': doc.data().name,
                    'details': newDetails,
                    'image': newImage,
                    'timestamp': moment(Date.now()).format('lll')
                })
            })
        })
    })

제 목표는 타임스탬프 순서에 따라 각 배열 항목을 DOM에 렌더링하는 것입니다.처음에 다음 Vuex 작업을 설정하려고 했습니다(아래 다시 참조)..orderBy()이 목표를 달성하기 위한 기능:

setCurrentUser: async context => {
    let ref = db.collection('users').orderBy('events')
    let snapshot = await ref.where('user_id', '==', firebase.auth().currentUser.uid).get()
    const users = []
    snapshot.forEach(doc => {
        let currentUserData = doc.data()
        currentUserData.id = doc.id
        users.push(currentUserData)
    })
    context.commit('setCurrentUser', users)
},

그러나 어레이 유형 doc 항목 내부의 타임스탬프를 기준으로 이 항목들을 주문하는 것은 불가능할 수도 있다는 것을 이해합니다.어레이 유형 문서 항목을 타임스탬프를 어레이 개체로 할당하지 않고 Firestore의 각 어레이 항목에 타임스탬프를 할당하는 방법에 대한 권장 사항이 있습니까?

다음은 Cloud Firestore에 사용할 수 있는 데이터 유형입니다. https://firebase.google.com/docs/firestore/manage-data/data-types

사용할 것을 제안합니다.Map타임스탬프를 기준으로 이벤트를 정렬하는 문제를 해결합니다.

옵션 1: Map<Timestamp, Event>동시에 이벤트가 없을 경우 키는 타임스탬프가 될 수 있습니다.키를 다음과 같이 설정하는 이유Timestamp왜냐하면Map정렬 기준Key그 다음에Value클라우드 파이어 스토어에서.

옵션 2: 한 사용자에 대해 동시에 둘 이상의 이벤트가 있을 수 있는 경우, 키는Event그래서 지도는 이렇게 보일 것입니다.Map<Event,Timestamp>검색 후 값을 기준으로 정렬할 수 있습니다.Map파이어스토어에서.

옵션 3: 모든 이벤트를 다른 컬렉션에 저장할 수 있는 경우Events그 다음에documentId사건이 열쇠가 될 수 있습니다.그래서 그Map되지요Map<String,Timestamp>사용자가 많은 이벤트를 가질 수 있다면 이 옵션을 제안합니다.

이게 도움이 되길 바랍니다.

언급URL : https://stackoverflow.com/questions/57861186/how-to-assign-a-timestamp-to-an-array-type-doc-item-in-firebase