英文:
Next TS MongoDb Error Post Request failed
问题
I'm just starting out learning MongoDB and following the official tutorial on YT and ran into an error when trying to connect and run a function. This is my code:
"use client"
import { useEffect, useState } from 'react'
import * as Realm from "realm-web"
export default function Home() {
const [products, setProducts] = useState([]);
useEffect(()=>{
const FetchProducts = async()=>{
const REALM_APP_ID = "{myAppId}";
const app = new Realm.App({id:REALM_APP_ID});
const credentials = Realm.Credentials.anonymous();
try{
const user = await app.logIn(credentials);
const allProducts = await user.functions.getAllProducts();
setProducts(allProducts);
console.log(allProducts);
}catch(error){
console.log(error);
}
}
FetchProducts();
},[])
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
{products && products.map(product =>(
<p key={product['_id']} className='text-gray-200'>{product['name']}</p>
))}
</main>
)
}
This code displays all the items within the YouTube tutorial, but I am getting an error:
Error: Request failed (POST https://us-east-1.aws.realm.mongodb.com/api/client/v2.0/app/products-pqfns/functions/call): function not found: 'getAllProducts' (status 404)
at MongoDBRealmError.fromRequestAndResponse (webpack-internal:///(:3000/app-client)/./node_modules/realm-web/dist/bundle.dom.es.js:2871:24)
at async Fetcher.fetch (webpack-internal:///(:3000/app-client)/./node_modules/realm-web/dist/bundle.dom.es.js:3034:23)
at async Fetcher.fetchJSON (webpack-internal:///(:3000/app-client)/./node_modules/realm-web/dist/bundle.dom.es.js:3051:26)
at async FetchProducts (webpack-internal:///(:3000/app-client)/./src/app/page.tsx:25:37)
Also, I'm getting a POST error. Does anyone know what I'm doing wrong? Thanks.
PS- I have created the function getAllProducts()
within Atlas.
Edit:
I expanded this error, and it says this:
{error: "no matching rule found", error_code: "NoMatchingRuleFound",…}
英文:
I'm just starting out learning mongodb and following the official tutorial on YT and ran into an error when trying to connect and run a function.
This is my code:
"use client"
import { useEffect, useState } from 'react'
import * as Realm from "realm-web"
export default function Home() {
const [products, setProducts] = useState([]);
useEffect(()=>{
const FetchProducts = async()=>{
const REALM_APP_ID = "{myAppId}";
const app = new Realm.App({id:REALM_APP_ID});
const credentials = Realm.Credentials.anonymous();
try{
const user = await app.logIn(credentials);
const allProducts = await user.functions.getAllProducts();
setProducts(allProducts);
console.log(allProducts);
}catch(error){
console.log(error);
}
}
FetchProducts();
},[])
return (
<main className="flex min-h-screen flex-col items-center justify-between p-24">
{products && products.map(product =>(
<p key={product['_id']} className='text-gray-200'>{product['name']}</p>
))}
</main>
)
}
This code displays all the items within the YT tutorial but I am getting an error of:
Error: Request failed (POST https://us-east-1.aws.realm.mongodb.com/api/client/v2.0/app/products-pqfns/functions/call): function not found: 'getAllProducts' (status 404)
at MongoDBRealmError.fromRequestAndResponse (webpack-internal:///(:3000/app-client)/./node_modules/realm-web/dist/bundle.dom.es.js:2871:24)
at async Fetcher.fetch (webpack-internal:///(:3000/app-client)/./node_modules/realm-web/dist/bundle.dom.es.js:3034:23)
at async Fetcher.fetchJSON (webpack-internal:///(:3000/app-client)/./node_modules/realm-web/dist/bundle.dom.es.js:3051:26)
at async FetchProducts (webpack-internal:///(:3000/app-client)/./src/app/page.tsx:25:37)
Also getting a Post Error. Doesn't anyone know what I'm doing wrong? Thanks.
PS- I have created the function getAllProducts()
within atlas
edit:
I expanded this error and it says this:
{error: "no matching rule found", error_code: "NoMatchingRuleFound",…}
答案1
得分: 0
你传递的是一个字符串,而不是实际的 Realm ID。
尝试将你的 Realm ID 保存到 .env.local 中,然后直接将其作为 process.env.REALM_APP_ID 传递,而不是使用 "{myAppId}"。这样更安全。
const REALM_APP_ID = process.env.REALM_APP_ID;
.env.local
REALM_APP_ID = "实际的 ID 在这里";
英文:
Your passing a string instead of the actual Realm ID
Try saving your Realm ID to .env.local and then pass it directly to REALM_APP_ID as process.env.REALM_APP_ID instead of doing this "{myAppId}". It's safer that way.
const REALM_APP_ID = process.env.REALM_APP_ID;
.env.local
REALM_APP_ID = "actual id here";
答案2
得分: 0
以下是翻译好的内容:
"Nothing was wrong with the code, to fix this issue I had to do a device sync which was not shown in the tutorial. This fixed my error and I am receiving the data."
请注意,代码部分未翻译。
英文:
Nothing was wrong with the code, to fix this issue I had to do a device sync which was not shown in the tutorial. This fixed my error and I am receiving the data.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论