英文:
REACT - Supabase
问题
我正在运行这段代码:
import { useEffect, useState } from 'react';
import { createClient } from '@supabase/supabase-js';
const supabase = createClient("https://<project>.supabase.co", '<anon-key>');
const GetBalance = () => {
const [data, setData] = useState([]);
useEffect(() => {
getData();
}, []);
async function getData() {
const { data: udata } = await supabase.from("balances").select();
setData(udata);
}
return (
<ul>
{data.map(transactions => (
<li key={transactions.balance}>{transactions.balance}</li>
))}
</ul>
)
}
export default GetBalance;
它返回了'Cannot read properties of undefined (reading 'map')'的错误。
我已经在supabase上禁用了RLS,并且表中包含数据且不为空。
这是我的代码中的语法错误吗?还是我配置了supabase项目不正确?
我在网上搜索了并禁用了RLS,但没有任何效果。
我尝试连接到我的supabase数据库,它可以连接但无法显示数据。然后我禁用了RLS,结果出现了这个错误。
英文:
I am Running this piece of code:
import { useEffect, useState } from 'react';
import { createClient } from '@supabase/supabase-js';
const supabase = createClient("https://<project>.supabase.co", '<anon-key>');
const GetBalance = () => {
const [data, setData] = useState([]);
useEffect(() => {
getData();
}, []);
async function getData() {
const { udata } = await supabase.from("balances").select();
setData(udata);
}
return (
<ul>
{data.map(transactions => (
<li key={transactions.balance}>{transactions.balance}</li>
))}
</ul>
)
}
export default GetBalance;
it responds with 'Cannot read properties of undefined (reading 'map').'
I have disables RLS on supabase and the table does include data and is not empty.
Is this a syntactical error in my code? or have I configured my supabase project incorrectly?
I have searched online and disables RLS however it has done nothing
I was trying to connect to my database on supabase but it would connect but not display data. I then disables RLS and it spits out this error
答案1
得分: 0
首先,你可以使用console.log
来打印出"data",看看是否有数据返回。看起来"data"是未定义的。你也可以尝试写下面的代码:
{data?.map((transactions) => (
<li key={transactions.balance}>{transactions.balance}</li>
))}
此外,我相信你有意将下面一行中的实际项目ID和密钥值替换为<project>和<anon-key>,但我只是确认一下:
const supabase = createClient("https://<project>.supabase.co", '<anon-key>');
当然,这些值应该是你从项目仪表板中获取的。
英文:
The first step would be to console.log
'data' to see if you are getting any. Seems like the 'data' is undefined. You could also try writing this:
{data?.map((transactions) => (
<li key={transactions.balance}>{transactions.balance}</li>
))}
Also, I believe you intentionally replaced the actual project id and key values in the line below with <project> and <anon-key>, but just making sure:
const supabase = createClient("https://<project>.supabase.co", '<anon-key>');
. Of course, these should be values you get from your project dashboard.
答案2
得分: 0
我发现如果我将这行代码:
const { udata } = await supabase.from("balances").select();
改为:
const { data: udata } = await supabase.from("balances").select();
它应该可以正常工作,因为这是一个变量命名的问题。
英文:
I figured that if i change the line
const { udata } = await supabase.from("balances").select();
to
const { data: udata } = await supabase.from("balances").select();
it would work as it was a variable issue with the nameing
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论