Error: Some functionality, such as asynchronous I/O, timeouts, and generating random values,...

"Error: Some functionality, such as asynchronous I/O, timeouts, and generating random values, can only be performed while handling a request.\n at g.fetch (__next-on-pages-dist__/chunks/411.js:2:4658)\n at k.fetch (__next-on-pages-dist__/chunks/411.js:7:14205)\n at k.fetchJSON (__next-on-pages-dist__/chunks/411.js:7:14647)\n at get locationUrl [as locationUrl] (__next-on-pages-dist__/chunks/411.js:7:17751)\n at get locationUrl [as locationUrl] (__next-on-pages-dist__/chunks/411.js:7:15116)\n at Et.getLogInUrl (__next-on-pages-dist__/chunks/411.js:7:11828)\n at Et.authenticate (__next-on-pages-dist__/chunks/411.js:7:11080)\n at b.logIn (__next-on-pages-dist__/chunks/411.js:7:16697)\n at new Ur (__next-on-pages-dist__/chunks/568.js:348:52621)\n at get instance [as instance] (__next-on-pages-dist__/chunks/568.js:348:52735)"
"Error: Some functionality, such as asynchronous I/O, timeouts, and generating random values, can only be performed while handling a request.\n at g.fetch (__next-on-pages-dist__/chunks/411.js:2:4658)\n at k.fetch (__next-on-pages-dist__/chunks/411.js:7:14205)\n at k.fetchJSON (__next-on-pages-dist__/chunks/411.js:7:14647)\n at get locationUrl [as locationUrl] (__next-on-pages-dist__/chunks/411.js:7:17751)\n at get locationUrl [as locationUrl] (__next-on-pages-dist__/chunks/411.js:7:15116)\n at Et.getLogInUrl (__next-on-pages-dist__/chunks/411.js:7:11828)\n at Et.authenticate (__next-on-pages-dist__/chunks/411.js:7:11080)\n at b.logIn (__next-on-pages-dist__/chunks/411.js:7:16697)\n at new Ur (__next-on-pages-dist__/chunks/568.js:348:52621)\n at get instance [as instance] (__next-on-pages-dist__/chunks/568.js:348:52735)"
Runs fine on local deployment with edge configurations, i'm not quite sure why i'm encountering this error with cloudflare pages.
35 Replies
smultar
smultarOP2y ago
:0
smultar
smultarOP2y ago
smultar
smultarOP2y ago
MY SAVIOR IS HERE
James
James2y ago
Afraid not haha, I'm not sure what the cause might be Please could you open an issue on the next-on-pages repository on GitHub for this issue. If possible, it would be very much appreciated if a reproduction could be provided along with it.
smultar
smultarOP2y ago
cries
James
James2y ago
Just please try and provide as much information about your project as possible, or a reproduction 🙏🏻. It might be caused by a dependency doing something in the global scope, but hard to say without being able to dig in
smultar
smultarOP2y ago
What confuses me if, its build and publishes. I am doing stuff in the global scope
import * as Realm from "realm-web";

const uri = process.env.DB_URI as string; // your mongodb connection string
const options = {};

declare global {
var _mongoClientPromise: Promise<Realm.User>;
}

class Singleton {
private static _instance: Singleton;
private client: Realm.App;
private clientPromise: Promise<Realm.User>;
private constructor() {
this.client = new Realm.App({ id: process.env.DB_APP_ID as string });
this.clientPromise = this.client.logIn(Realm.Credentials.apiKey(process.env.DB_API_KEY as string));
if (process.env.NODE_ENV === 'development') {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
global._mongoClientPromise = this.clientPromise;
}
}

public static get instance() {
if (!this._instance) {
this._instance = new Singleton();
}
return this._instance.clientPromise;
}
}
const clientPromise = Singleton.instance;

// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise;
import * as Realm from "realm-web";

const uri = process.env.DB_URI as string; // your mongodb connection string
const options = {};

declare global {
var _mongoClientPromise: Promise<Realm.User>;
}

class Singleton {
private static _instance: Singleton;
private client: Realm.App;
private clientPromise: Promise<Realm.User>;
private constructor() {
this.client = new Realm.App({ id: process.env.DB_APP_ID as string });
this.clientPromise = this.client.logIn(Realm.Credentials.apiKey(process.env.DB_API_KEY as string));
if (process.env.NODE_ENV === 'development') {
// In development mode, use a global variable so that the value
// is preserved across module reloads caused by HMR (Hot Module Replacement).
global._mongoClientPromise = this.clientPromise;
}
}

public static get instance() {
if (!this._instance) {
this._instance = new Singleton();
}
return this._instance.clientPromise;
}
}
const clientPromise = Singleton.instance;

// Export a module-scoped MongoClient promise. By doing this in a
// separate module, the client can be shared across functions.
export default clientPromise;
James
James2y ago
Oh yep, that's it
smultar
smultarOP2y ago
Soo I guess Don't do this at all?
James
James2y ago
Yeah, you should try and move it to happen at request-time instead
smultar
smultarOP2y ago
I'll go ahead and try to figure out how to rewrite this without global scope... Im still new to typescript And this example was given to me But, hey were going somewhere!
James
James2y ago
Yeah, I mean, that would probably be useful to use in a next.js nodejs server, but it wouldn't be that helpful in a serverless environment. The idea behind writing the realm client like that is so it would only be instantiated once, which works well for lifelong nodejs servers, but in a serverless environment where everything exists for the duration of the request, well, the effect isn't really quite the same
smultar
smultarOP2y ago
Sadly, I can't get mongodb to work in the edge runtime any other way @Better James To my currently level of knowledge Made the changes, heres to hoping :> Also happy 4th of july @Better James
James
James2y ago
I'm British 😠
smultar
smultarOP2y ago
OH NO Im sorry!
James
James2y ago
Here's an old example that you might be able to take some inspiration from - https://github.com/mongodb-developer/cloudflare-worker-rest-api-atlas/blob/main/src/index.ts
smultar
smultarOP2y ago
Same results.
import * as Realm from "realm-web";

// Create a promise that resolves with the MongoClient.
type client = Realm.App;
type clientPromise = Promise<Realm.User>;

const Client: client = new Realm.App({ id: process.env.DB_APP_ID as string });
const ClientPromise: clientPromise = Client.logIn(Realm.Credentials.apiKey(process.env.DB_API_KEY as string));

export default ClientPromise;
import * as Realm from "realm-web";

// Create a promise that resolves with the MongoClient.
type client = Realm.App;
type clientPromise = Promise<Realm.User>;

const Client: client = new Realm.App({ id: process.env.DB_APP_ID as string });
const ClientPromise: clientPromise = Client.logIn(Realm.Credentials.apiKey(process.env.DB_API_KEY as string));

export default ClientPromise;
This is all I did. Is there a way to run a fake cloudflare pages environment on my pc @Better James ? Some users have described this.
smultar
smultarOP2y ago
smultar
smultarOP2y ago
But i'm unsure of where I would even begin to try messing arround with those options.
James
James2y ago
That's basically the same as what you had before. In the example above, they're doing it at request time oh and yeah, npx wrangler pages dev .vercel/output/static --compatibility-flag=nodejs_compat
smultar
smultarOP2y ago
any other specific params?
smultar
smultarOP2y ago
smultar
smultarOP2y ago
James
James2y ago
you have to build it with npx @cloudflare/next-on-pages first 😛
smultar
smultarOP2y ago
thx Could help me understand why this doesn't work on run time? Is it because of the promise?
James
James2y ago
I believe so
smultar
smultarOP2y ago
Is their a work arround for this kind of stuff? Cause, I would need to wait for the connection to succeed before doing anything else? When building, I have encountered another issue....
▲ Created all serverless functions in: 1.824s
▲ Error: Unable to find lambda for route: /search
▲ ERROR  Command failed with exit code 1: vercel build
▲ Created all serverless functions in: 1.824s
▲ Error: Unable to find lambda for route: /search
▲ ERROR  Command failed with exit code 1: vercel build
@Better James /search is a static page
James
James2y ago
Are you using Windows? I believe that's a bug with the Vercel CLI when using Windows. It stopped happening to me once I switched to Ubuntu via WSL
smultar
smultarOP2y ago
oh time to use wsl Do you think its cause of this..
// Database Object
let db = (await client.logIn(Realm.Credentials.apiKey(process.env.DB_API_KEY as string))).mongoClient('aetherlink');
// Database Object
let db = (await client.logIn(Realm.Credentials.apiKey(process.env.DB_API_KEY as string))).mongoClient('aetherlink');
James
James2y ago
the lambda route error? nah, that's just a windows bug in the vercel cli that i've encountered many times wouldnt be able to tell you what causes it, other than using windows lol
smultar
smultarOP2y ago
I'm loosing my mind with this bug. I can't seem to find any solution at all anywhere Hey, do you know if the edge runtime at vercel is different from cloudflare pages? With vercel, i'm able to use the stock mongodb library, but in cloudflare pages, im unable to run or utilize it.
James
James2y ago
Vercel's edge deployments are run on Cloudflare Workers through their infra using Workers for Platforms. Differences between how they work will be due to either how we process stuff, or any additional work they do before running the function. Is it the code you send above that is running on Vercel successfully?
smultar
smultarOP2y ago
So actually I changed the code above to mongodb's driver instead It works locally And on vercel but on cloudflare pages, it screams I need to have export const runtime = 'edge'; When using this, the default driver doesn't work anymore.
James
James2y ago
If it's not on the edge runtime, Vercel is deploying it to nodejs Lambda, not Workers with the edge runtime
smultar
smultarOP2y ago
I just want to get mongodb to work in the crying ya boi, back at it again @Better James I'm trying to figure out what it is, I need to do, to get this to work.
Want results from more Discord servers?
Add your server