. Advertisement .
..3..
. Advertisement .
..4..
NestJS is a NodeJS framework for developing scalable and efficient server-side applications. NestJS uses TypeScript for development, but it also supports Javascript.
When attempting to complete your task, you receive the following notification: “UnhandledPromiseRejectionWarning: Error: You must await server.start() before calling server.applyMiddleware() at ApolloServer“.
This is one of the most wide-known errors that any programmer will make. So, why does it appear, and how can it be resolved? We’ll go over it with you.
Why Does The Issue “UnhandledPromiseRejectionWarning: Error: You must await server.start() before calling server.applyMiddleware() at ApolloServer” Occur?
You are attempting to create my nextJs server and you encounter the error messages:
UnhandledPromiseRejectionWarning: Error: You must await server.start() before calling server.applyMiddleware() at ApolloServer
In this case, the server object, which is the Apollo server object, is an async function. It indicates that the middle of the following line of code will run before the server has finished initializing. That is the reason why you encounter the “UnhandledPromiseRejectionWarning: Error: You must await server.start() before calling server.applyMiddleware() at ApolloServer” error.
The Best Methods For ”UnhandledPromiseRejectionWarning: Error: You must await server.start() before calling server.applyMiddleware() at ApolloServer” Issue
And guess what, we just solved it using some methods listed below.
Method 1: Use “apollo-server-express”: “^3.0.0”
This first solution to handle “UnhandledPromiseRejectionWarning: Error: You must await server.start() before calling server.applyMiddleware() at ApolloServer” problem is using “apollo-server-express“: “^3.0.0”. And there already is a workaround.
Method 2: Downgrade to apollo-server-express@2
The second solution is very simple. This is a known problem, including an open ticket. As a result, for the time being, you could even downgrade to apollo-server-express@2.
Method 3: Add an await
This problem also occurred when you upgraded Ben Awad’s Graphql-Next-Typeorm[…] stack, easily going to add an await to the server and begin correcting the warnings. Please see the script below.
const apolloServer = new ApolloServer({
introspection: true,
schema: await buildSchema({
resolvers: [__dirname + '/resolvers/**/*.js'],
validate: false
}),
context: ({ req, res }) => ({
req,
res,
redis: redisClient
}),
formatError
});
// added this line
await apolloServer.start();
apolloServer.applyMiddleware({
app,
cors: false
});
Now, we can assure you that your issue should be tackled.
Method 4: Wrap middleware in a async function
Besides the mentioned solutions above, wrapping middleware in a async function is a great method for solving the “UnhandledPromiseRejectionWarning: Error: You must await server.start() before calling server.applyMiddleware() at ApolloServer” error. Look at the following example to further understand about this solution.
....
const app = express();
server.start().then(res => {
server.applyMiddleware({ app });
app.listen({ port: 3000 }, () =>
console.log('Now browse to http://localhost:4000' + server.graphqlPath)
)
})
Your error will be completely resolved after you doing this.
Conclusion
Nest.js is a server-side Node.js framework for building efficient, reliable, and scalable applications. It provides backend applications with a modular structure to organize code into separate modules. It was built to eliminate disorganized code bases.
For individuals who are still confused by this error: “UnhandledPromiseRejectionWarning: Error: You must await server.start() before calling server.applyMiddleware() at ApolloServer” the solutions listed above are the quickest.
If you still need help or have general NestJs questions, we have a thriving community where everyone is always willing to help. Finally, we wish you a successful day filled with new solutions and code.
Read more
Leave a comment