Sign in
Log inSign up

How to use MongoDB Stitch with Next.js

faisal ansari's photo
faisal ansari
·Apr 10, 2019

Hi All,

I have planned to use mongoDB Stitch. But it provides browser SDK and server SDK. And tried both of them and i am getting some error. I believe this is because my app is isomorphic as i am using Next.js. Please guide me what can i do in such situation

 > Ready on http://localhost:3000

Note that pages will be compiled when you first load them.
ReferenceError: self is not defined
    at D:\simpliask\node_modules\whatwg-fetch\dist\fetch.umd.js:8:40
    at support.searchParams (D:\simpliask\node_modules\whatwg-fetch\dist\fetch.umd.js:2:66)
    at Object.<anonymous> (D:\simpliask\node_modules\whatwg-fetch\dist\fetch.umd.js:5:2)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (D:\simpliask\node_modules\mongodb-stitch-browser-core\dist\cjs\core\internal\net\BrowserFetchTransport.js:4:22)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)

And when i use mongodb-stitch-server-sdk i get following exception.

Failed to compile. ./node_modules/fs-extra/lib/index.js Module not found: Can't resolve 'fs' in 'D:\simpliask\node_modules\fs-extra\lib'

I am using server sdk in getInitialProps as below.

Index.getInitialProps = async function () {
 const {Stitch}=require('mongodb-stitch-server-sdk');
    const client = Stitch.initializeDefaultAppClient('*********')

    return {
        clientd:client
    }
  }