下一个TS MongoDb错误:Post请求失败

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

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:

&quot;use client&quot;
import { useEffect, useState } from &#39;react&#39;
import * as Realm from &quot;realm-web&quot;

export default function Home() {
  const [products, setProducts] = useState([]);

  useEffect(()=&gt;{
    const FetchProducts = async()=&gt;{
      const REALM_APP_ID = &quot;{myAppId}&quot;;
      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 (
    &lt;main className=&quot;flex min-h-screen flex-col items-center justify-between p-24&quot;&gt;
      {products &amp;&amp; products.map(product =&gt;(
        &lt;p key={product[&#39;_id&#39;]} className=&#39;text-gray-200&#39;&gt;{product[&#39;name&#39;]}&lt;/p&gt;
      ))}
    &lt;/main&gt;
  )
}

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: &#39;getAllProducts&#39; (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: &quot;no matching rule found&quot;, error_code: &quot;NoMatchingRuleFound&quot;,…}

答案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 = &quot;actual id here&quot;;

答案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.

huangapple
  • 本文由 发表于 2023年6月9日 00:28:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76433951.html
匿名

发表评论

匿名网友

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

确定