【问题标题】:Sort Data Using Time Value in Realtime Database using React-js使用 React-js 在实时数据库中使用时间值对数据进行排序
【发布时间】:2021-05-22 19:08:26
【问题描述】:

我正在尝试根据创建时间对我的 Firebase 数据进行排序,以便最后创建的条目应位于顶部。 下面是我的代码,

import React, { useEffect, useState } from 'react'
import fire from './fire1';
export const Weight = () => {

const[input, setInput] = useState("")
const[weightList, setWeightList] = useState("")
const handleOnSubmit = () =>{
    const dataRef = fire.database().ref('Tushar');
    const data = ({
        input,
        time : new Date().toLocaleTimeString()
    });
    dataRef.push(data)
    console.log(dataRef.orderBy('currTime').get())
}
useEffect(()=>{
    
    const dataRef =  fire.database().ref('Tushar');
    dataRef.on('value', (snapshot)=>{
        var weight = (snapshot.val())
        var weightList = []
        for (let id in weight){
            weightList.push(weight[id])
        }
        setWeightList(weightList)
        
    })
},[])

return (
    <div>
            <input
            type = "text"
            placeholder = "Enter Your Weight"
            value = {input}
            onChange = {(e)=> setInput(e.target.value)}
            />
            <button onClick = {handleOnSubmit} >Submit</button>
        {weightList? weightList.map((val, index) =>{
            return(
                <div key = {index} >
            <h1>{val.input}</h1>
            <i>{val.time}</i>    
                </div>
            )
        }): "" }
    </div>
)
}

当我尝试使用 OrderBy() 函数时 它显示以下错误。

TypeError: dataRef.orderBy 不是函数

如果我犯了任何错误或遗漏了某些信息,请告诉我。

【问题讨论】:

    标签: javascript reactjs firebase react-native firebase-realtime-database


    【解决方案1】:

    Firebase real time database documentation

    读取数据部分

    您尝试使用的查询是 firebase firestore 数据库,而不是实时数据库,

    这些是实时数据库的查询

    orderByChild()  : Order results by the value of a specified child key or nested child path.
    orderByKey()    : Order results by child keys.
    orderByValue()  : Order results by child values.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-08
      • 2021-03-16
      • 1970-01-01
      • 2019-05-04
      • 1970-01-01
      • 2022-01-17
      • 2011-03-18
      • 1970-01-01
      相关资源
      最近更新 更多