next.js 下一个TS MongoDb错误发布请求失败

9vw9lbht  于 2023-06-22  发布在  Go
关注(0)|答案(2)|浏览(107)

我刚开始学习mongodb,并遵循YT的官方教程,在尝试连接和运行函数时遇到了一个错误。这是我的代码:

"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>
  )
}

此代码显示YT教程中的所有项目,但我得到一个错误:

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)

也有一个Post Error。没人知道我做错了什么吗谢谢。PS-我已经在atlas中创建了函数getAllProducts()
编辑:我扩展了这个错误,它说:{error: "no matching rule found", error_code: "NoMatchingRuleFound",…}

ngynwnxp

ngynwnxp1#

您传递的是字符串而不是实际的领域ID
尝试将您的领域ID保存到.env.local,然后将其作为process.env.REALM_APP_ID直接传递给REALM_APP_ID,而不是执行此操作“{myAppId}”。这样更安全

const REALM_APP_ID = process.env.REALM_APP_ID;

.env.local

REALM_APP_ID = "actual id here";
6yt4nkrj

6yt4nkrj2#

代码没有任何问题,为了解决这个问题,我不得不做一个设备同步,这在教程中没有显示。这修复了我的错误,我正在接收数据。

相关问题