Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: resolver is not a function

I am attempting to create a basic post on click in my NextJS app to a MongoDB database. The issue i am getting is TypeError: resolver is not a function. I understand it might be a syncronicity issue but for the life of me I cannot figure out where.

Stack used: NextJS, Axios, Mongoose.

Component code snippet calling axios:

i know the states are updating so i am putting only the snippet that handles the request

  handleSubmit = async (e: any) => {
    e.preventDefault();

    await axios
      .post('/api/roomSession', {
        roomName: this.state.roomName,
        teamName: this.state.teamName
      })
      .then((response: any) => console.log('axios call reached', response))
      .catch((error: any) => console.log('---- error! ----', error));
  };

[...]
<button onClick={this.handleSubmit}>Create</button>
[...]

NextJS API file:

import { newSession } from '../../packages/backend/mongo/connection';

const apiNewSession = async (roomName, teamName) => {
  await newSession(teamName, roomName);
};

export default apiNewSession();

Mongoose file:

const mongoose = require('mongoose');

mongoose
  .connect(
    'mongodbconnection',
    { useNewUrlParser: true, useUnifiedTopology: true }
  )
  .then(() => {
    console.log('connected to mongoDB');
  })
  .catch(err => console.error('Connection failed: ', err));

  const sessionSchema = new mongoose.Schema({
    teamName: String,
    roomName: String
  });

const Session = mongoose.model.tests || mongoose.model('tests', sessionSchema);

export const newSession = async (teamName, roomName) => {
  const session = new Session({
    teamName,
    roomName
  });
  const result = await session.save();
  mongoose.connection.close();
};

Some extra info on the strange behaviour: When first time called, the code throws the aformentioned error but manages to reach the mongoDB connection and creates an EMPTY entry inside the collection (only _id, and _v). Upon second attempt, only the error is thrown.

like image 690
Valentin Avatar asked Feb 15 '20 19:02

Valentin


People also ask

How do you resolve TypeError in react?

The React. js "Uncaught TypeError: X is not a function" occurs when we try to call a value that is not a function as a function, e.g. calling the props object instead of a function. To solve the error, console. log the value you are calling and make sure it is a function.

Is not a function TypeError is not a function?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.


1 Answers

As usual, the answer is easy once you put your mind to it.

I was exporting the function incorrectly from the NextJS API file.

Correct method: export default apiNewSession;

Not sure why it still happened when when i was exporting the function by default.

like image 147
Valentin Avatar answered Sep 18 '22 13:09

Valentin