TypeError: resolver is not a function

TypeError: resolver is not a function

我正在尝试在我的 NextJS 应用程序中点击创建一个基本的 post 到 MongoDB 数据库。我遇到的问题是 TypeError: resolver is not a function。我知道这可能是一个同步问题,但对于我来说我无法弄清楚在哪里。

使用的堆栈:NextJS、Axios、Mongoose。

调用 axios 的组件代码片段:

我知道状态正在更新,所以我只放了处理请求的片段

  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 文件:

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

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

export default apiNewSession();

猫鼬文件:

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();
};

关于奇怪行为的一些额外信息:第一次调用时,代码抛出上述错误但设法到达 mongoDB 连接并在集合中创建一个 EMPTY 条目(仅_id 和 _v)。 第二次尝试时,只会抛出错误。

像往常一样,只要您下定决心,答案就很简单。

我从 NextJS API 文件中错误地导出了函数。

正确方法: export default apiNewSession;

不确定为什么在我默认导出函数时仍然发生这种情况。