firebase实时数据库问题,equalTo()与react原生expo

ie3xauqp  于 2023-03-03  发布在  React
关注(0)|答案(1)|浏览(92)

我正在尝试随机化数据库中的节点,以便根据ShopNo查询
但我无法在使用equalTo时获得所需的节点。我只能根据数据库中的Shop数量获得随机值。有人能帮我吗?
我的数据库的一部分:

{
  "Bakery": {
    "Bakery Cuisine": {
      "Description": "Within North Spine Plaza",
      "Halal": "Yes",
      "Latitude": 1.34714,
      "Location": "50 Nanyang Ave, #01-20 North Spine Plaza, Singapore 639798",
      "Longitude": 103.68066,
      "OH": "Mon - Sat : 8 AM to 7 PM, Sun Closed",
      "ShopNo": 1
    }
  },
  "Beverage": {
    "Beverage": {
      "Description": "Within the South Spine food court",
      "Halal": "No",
      "Latitude": 1.34253,
      "Location": "21 Nanyang Link, Singapore 637371",
      "Longitude": 103.68243,
      "OH": "Mon - Fri: 7 30 am to 8 pm, Sat - Sun/PH Closed",
      "ShopNo": 2
    },
    "Beverages": {
      "Description": "Within North Spine Koufu",
      "Halal": "No",
      "Latitude": 1.34708,
      "Location": "76 Nanyang Dr, #02-03 North Spine Plaza, Singapore 637331",
      "Longitude": 103.68002,
      "OH": "Mon - Fri : 7 am to 8 pm, Sat : 7 am to 3 pm, Sun Closed",
      "ShopNo": 3
    },
    "Boost": {
      "Description": "Within North Spine Plaza",
      "Halal": "No",
      "Latitude": 1.34735,
      "Location": "50 Nanyang Ave, #01-11 North Spine Plaza, Singapore 639798",
      "Longitude": 103.68036,
      "OH": "Mon - Fri : 10 am to 9 pm, Sat - Sun: 10 am to 6 pm",
      "ShopNo": 4
    },
"Total": 89,
}

总计包含我在数据库中拥有的商店(子商店)数量。使用此总计,我可以生成一个随机值,但当我尝试查询数据库时,我得到以下错误:

我有一种感觉我的语法是错误的在这个问题上,但我不能找出正确的语法
我的代码:

import { StyleSheet, Text, View, TouchableOpacity } from 'react-native'
import { useNavigation } from '@react-navigation/core'
import {db} from '../firebase'
import React, {useEffect, useState} from 'react'
import {ref, get, orderByChild, equalTo} from 'firebase/database'

const SubScreen2 = () => {
  
  
  const navigation = useNavigation()
  
  const [todoData, setToDoData] = useState([])
  
  
  useEffect (() => {
    var random = 0
    get(ref(db, "food/Total")).then(snapshot => {
      const count = snapshot.val();
      random = Math.floor((Math.random() * count));
      console.log(random) //Random number generated (Successful)
      ref(db, `food/`), orderByChild("ShopNo"), equalTo(random)  //Failed
      .then(snapshot => {
        const data = snapshot.val() 
        setToDoData(data)
      })
    
      
  },[]) })
  console.log(todoData)

更新:从ref(db,food/)、orderByChild("ShopNo")、equalTo(随机)更改为查询(ref(db,food/)、orderByChild("ShopNo")、equalTo(随机)),并从firebase/数据库导入查询,因为它无法找到查询变量

import {ref, get, orderByChild, equalTo, query} from 'firebase/database'
const SubScreen2 = () => {
  
  
  const navigation = useNavigation()
  
  const [todoData, setToDoData] = useState([])
  
  
  useEffect (() => {
    var random = 0
    get(ref(db, "food/Total")).then(snapshot => {
      const count = snapshot.val();
      random = Math.floor((Math.random() * count));
      console.log(random)
      query(ref(db, `food/`), orderByChild("ShopNo"), equalTo(random))
      .then(snapshot => {
        const data = snapshot.val()
        setToDoData(data)
      })
    
      
  },[]) })
  console.log(todoData)

我仍然收到错误-TypeError:undefined不是函数(靠近..."ualTo")(随机))。then(函数(快照)...")

njthzxwz

njthzxwz1#

这看起来不对:

ref(db, `food/`), orderByChild("ShopNo"), equalTo(random)  //Failed
.then(snapshot => {
  ...

这里缺少一个query,它可能应该是:

query(ref(db, `food/`), orderByChild("ShopNo"), equalTo(random))
.then(snapshot => {
  ...

因此,您将引用、排序指令和筛选指令传递给query,然后根据结果promise调用then

相关问题