[SOLVED] Can I import multiple Moralis objects in the same component/page

Can I use multiple Moralis calls like that?

For example:

  • I want to get data by using Moralis-v1/node with SSR in NextJS, then send it to the component,
  • which must be able to use sign-in logic that comes with Moralis-v1 only

Code below has two different import of Moralis object with different names.

I try this but real code is complicated than that, so in order to reduce the errors I needed to ask.

import { useMoralis } from "react-moralis"
import MoralisNode from "moralis-v1/node"
import Moralis from "moralis-v1"

const Page = ({ thisItem }) => {
  const { authenticate, enableWeb3 } = useMoralis()
  const [authError, setAuthError] = useState(null)
  const [isAuthenticating, setIsAuthenticating] = useState(false)

  const handleAuth = async (provider) => {
    try {
      setAuthError(null)
      setIsAuthenticating(true)

      // Enable web3 to get user address and chain
      await enableWeb3({ throwOnError: true, provider })
      //...
    }
    //...
}

// SSR Props
export async function getServerSideProps({ params }) {
  const { itemSlug } = params

  await MoralisNode.start({
    serverUrl: process.env.SERVER_URL,
    appId: process.env.APP_ID,
  })

  // get item by its slug
  const item = await MoralisNode.Cloud.run("getItem", {
    slug: itemSlug,
  })

  return {
    props: {
      thisItem: item,
    },
  }
}

Answer is yes, it only requires a little tweak at the import lines:

import { useMoralis } from "react-moralis"
import { Moralis as MoralisNode } from "moralis-v1/node"
import Moralis from "moralis-v1"
//...

So, here MoralisNode is Moralis-v1/node object that is available for NextJS’s getServerSideProps().

For whom would like create NexsJS pages that retireves data from server with getServerSideProps(context)[^1] to use Moralis-v1 and react-moralis, together with the new authentication rules[^2] by Moralis-v1, with self-hosted server app[^3]. :white_check_mark:

[^1]:getServerSideProps in NextJS
[^2]:Updated Authentication Rules
[^3]:Self Hosted Server App

1 Like