[SOLVED] Loading moralis v1 in nextjs not working

I’m getting this error trying to load moralisv1 in nextjs… I’ve whitelisted localhost in my admin

return(
<Script
src=“https://unpkg.com/moralis-v1/dist/moralis.js
strategy=“lazyOnload”
onLoad={() =>
console.log(‘script loaded correctly, moralis is good to go’)
}/>
)
Access to XMLHttpRequest at ‘https://wnvkqhotzhox.usemoralis.com:2053/server/functions/getPluginSpecs’ from origin ‘http://localhost:3000’ has been blocked by CORS policy: Response to preflight request doesn’t pass access control check: No ‘Access-Control-Allow-Origin’ header is present on the requested resource.

It works normally without any whitelisted domains? Can you screenshot your exact whitelisted domains from your Moralis server Settings page.

I’m just having all types of problems using moralis with nextjs…
you guys have an example of the correct way to use hooks…

i can’t seem to get it right…

<WagmiConfig client={client}>
  <SessionProvider session={pageProps.session} refetchInterval={0}>
  <MoralisProvider appId="gRQ26K3vKSOUekuZzaJ2OcxC7VaGFpRyUdj4So7r" serverUrl="https://wnvkqhotzhox.usemoralis.com:2053/server">
  <Layout>
    <Component {...pageProps} />
  </Layout>
  </MoralisProvider>,
  </SessionProvider>
</WagmiConfig>

moralis is not defined when I do it this way…

trying to upload metadata to ipfs using v1

unless there’s a way to do it in v2

moralis is not defined when I do it this way…

How have you imported MoralisProvider or Moralis in any other file? Make sure you’re using [email protected] and [email protected].

Make sure to follow the instructions here for React (Next.js you wrap the provider around <Component /> like you have done).

unless there’s a way to do it in v2

You can use uploadFolder but I assume you want to use Moralis servers instead of your own backend.


followed the instructions here… Moralis react: saveFile() on IPFS

and I keep getting this erro

no examples on how to use this with react?

Yes you can check out this hook and the rest of the docs.

ipfs upload throws moralis undefined error

You need to import Moralis somewhere in your component if you haven’t. You can use:

const { Moralis } = useMoralis();

You need to call Moralis.start with an applicationId before using Moralis. even though the provider is added as in the example about…

You can use isInitialized from the useMoralis hook, you are probably trying to run this code immediately on app/component load - this error will happen if the server connection hasn’t finished.

useEffect(() => {
  if (isInitialized) // call any Moralis code
}, [isInitialized]);

followed the instructions here… Moralis react: saveFile() on IPFS

and I keep getting this erro

it’s trying to post it to …
https://wnvkqhotzhox.usemoralis.com:2053/server/files/testing

that’s obviously not right

error is here… still not initializing?

  return _promise.default.resolve(null);
}).then(function (token) {
  if (token) {
    payload._SessionToken = token;
  }

Can you post your full Moralis code you’re using now, including your _app.js file.

_app.js

import React from 'react'
import Head from 'next/head'
import Layout from '../components/layout'
import "bootstrap/dist/css/bootstrap.min.css";
import '../styles/global.css'
import '../styles/remixicon.css'
import { useEffect } from "react";
import { createClient, configureChains, defaultChains, WagmiConfig } from 'wagmi';
import { publicProvider } from 'wagmi/providers/public';
import { SessionProvider } from 'next-auth/react';
import { InjectedConnector } from 'wagmi/connectors/injected'
import { MoralisProvider } from "react-moralis";

const { chains, provider } = configureChains(defaultChains, [
  publicProvider()
])
const client = createClient({
  provider,
  SessionProvider,
  autoConnect: true,
  connectors: [new InjectedConnector({ chains })]
});


function MyApp({ Component, pageProps }) {
  useEffect(() => {
  require("bootstrap/dist/js/bootstrap.bundle.min.js");
}, []);
  return (
    <WagmiConfig client={client}>
      <SessionProvider session={pageProps.session} refetchInterval={0}>
        <MoralisProvider appId="gRQ26K3vKSOUekuZzaJ2OcxC7VaGFpRyUdj4So7r" serverUrl="https://wnvkqhotzhox.usemoralis.com:2053/server" initializeOnMount={true}>
          <Layout>
            <Component {...pageProps} />
          </Layout>
        </MoralisProvider>,
      </SessionProvider>
    </WagmiConfig>
  )
}

export default MyApp

component

import React from 'react'
import Image from 'next/image';
import Link from 'next/link';
import utilStyles from '../styles/utils.module.css';
import cn from 'classnames';
import {useCallback, useEffect, useState} from 'react';
import { utils, BigNumber, ContractTransaction } from 'ethers';
import { useNotification, Icon, Loading } from 'web3uikit';
import { getSession, signOut } from 'next-auth/react';
import _nftea from '../lib/contracts/NFTEAS.json'
import _labels from '../lib/contracts/LABELS.json'
import { useContractRead, useContractWrite, usePrepareContractWrite } from 'wagmi'
import { useMoralis,useMoralisFile } from "react-moralis";
import { Moralis } from 'moralis'

const HomePage = () => {
  const { initialize, isInitialized } = useMoralis()

  const {saveFile, moralisFile} = useMoralisFile()
  console.log(isInitialized) 

  const handleUpload = async (event)=> {

          console.log(event.target.files[0])

          const toFile = event.target.files[0];

          console.log(toFile)

          if(toFile.type=='image/heic'){


          }else{

            if(toFile.type.startsWith('image')){

              // setImage(toFile)
              const _imageurl = await saveFile('testing', toFile, {saveIPFS: false})
              console.log(_imageurl)

              // const _saveimage = await saveFile(toFile.name, toFile, {saveIPFS: true})
              // console.log('image saved ' + _saveimage);
              // setImage(_saveimage)

            }else{

              // setMedia(toFile)

            }

        }
  }
}

it is initailizing

save to ipfs is true… i turned it off to see what happens

seems like it doesn’t know I already authenticated using wagmi

You need to be authenticated to your Moralis server (Moralis.authenticate() or use authenticate from useMoralis hook) to use Moralis.saveIPFS() or the saveFile() hook.

thanks works with moralis.authenticate… I’ll remove all the wagmi stuff… your new documentation recommends wagmi… I could have just use moralis authenticate from the start…

your new documentation recommends wagmi

You were reading the docs for Moralis v2 / 2.0, Moralis.authenticate() is for moralis-v1 or using Moralis servers.

Make sure to only reference v1 docs.