Supabase仅返回1行(nodejs)

huangapple go评论69阅读模式
英文:

Supabase returns only 1 row (nodejs)

问题

I'm a Supabase newbie.

我是一个 Supabase 新手。

I'm getting the whole arrays fine on the first visit of the root '/'

第一次访问根路径'/'时,我可以正常获取整个数组。

But once I visit the route for 1 specific restaurant '/:id', subsequent visits of '/' will always return only 1 row.

但是,一旦我访问了特定餐厅的路由'/:id',之后再次访问'/'将始终只返回一行。

Seems like it's caching it after the '/:id' query.

似乎在'/:id'查询之后对其进行了缓存。

Please tell me what I'm doing wrong.

请告诉我我做错了什么。

Thank you.

谢谢。

no-more-hair-to-pull

没有更多的头发可以拔

const express = require('express')
const router = express.Router()
const supabase = require('../provider/supabase')
const restaurants = supabase.from('restaurants')

router.get('/', async (req, res) => {
  const { data } = await restaurants.select()
  res.json(data)
})

router.get('/:id', async (req, res) => {
  const { id } = req.params
  const { data } = await restaurants.select().eq('id', id)

  if (!data) res.sendStatus(404)

  const restaurant = data.length > 0 ? data[0] : undefined

  if (!restaurant) {
    res.sendStatus(404)
  }
  res.json(restaurant)
})
const express = require('express')
const router = express.Router()
const supabase = require('../provider/supabase')
const restaurants = supabase.from('restaurants')

router.get('/', async (req, res) => {
  const { data } = await restaurants.select()
  res.json(data)
})

router.get('/:id', async (req, res) => {
  const { id } = req.params
  const { data } = await restaurants.select().eq('id', id)

  if (!data) res.sendStatus(404)

  const restaurant = data.length > 0 ? data[0] : undefined

  if (!restaurant) {
    res.sendStatus(404)
  }
  res.json(restaurant)
})
英文:

I'm a Supabase newbie.

I'm getting the whole arrays fine on the first visit of the root '/'

But once I visit the route for 1 specific restaurant '/:id', subsequent visits of '/' will always return only 1 row.

Seems like it's caching it after the '/:id' query.

Please tell me what I'm doing wrong.

Thank you.

no-more-hair-to-pull

const express = require('express')
const router = express.Router()
const supabase = require('../provider/supabase')
const restaurants = supabase.from('restaurants')

router.get('/', async (req, res) => {
  const { data } = await restaurants.select()
  res.json(data)
})

router.get('/:id', async (req, res) => {
  const { id } = req.params
  const { data } = await restaurants.select().eq('id', id)

  if (!data) res.sendStatus(404)

  const restaurant = data.length > 0 ? data[0] : undefined

  if (!restaurant) {
    res.sendStatus(404)
    
  }
  res.json(restaurant)
})

答案1

得分: 2

因为 Supabase 客户端库的结构,你目前不能将 supabase.from('restaurants') 保留为变量。这会记住之前使用的旧查询。解决方案是每当要构建新查询时,始终调用 from()

英文:

Because how the client Supabase client library is structured, you currently cannot keep supabase.from('restaurants') as a variable. This will remember the old query that was used against it. The solution here is to always call from() whenever you want to construct a new query.

const express = require('express')
const router = express.Router()
const supabase = require('../provider/supabase')

router.get('/', async (req, res) => {
  const { data } = await supabase.from('restaurants').select()
  res.json(data)
})

router.get('/:id', async (req, res) => {
  const { id } = req.params
  const { data } = await supabase.from('restaurants').select().eq('id', id)

  if (!data) res.sendStatus(404)

  const restaurant = data.length > 0 ? data[0] : undefined

  if (!restaurant) {
    res.sendStatus(404)
    
  }
  res.json(restaurant)
})

huangapple
  • 本文由 发表于 2023年4月20日 01:06:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057138.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定