【问题标题】:TypeError: resolver is not a functionTypeError:解析器不是函数
【发布时间】:2023-03-12 08:53:01
【问题描述】:

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

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

组件代码sn-p调用axios:

我知道状态正在更新,所以我只放置处理请求的 sn-p

  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)。 在第二次尝试时,只会抛出错误。

【问题讨论】:

    标签: javascript mongoose axios next.js


    【解决方案1】:

    像往常一样,只要你下定决心,答案就很容易了。

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

    正确方法: export default apiNewSession;

    不知道为什么当我默认导出函数时它仍然发生。

    【讨论】:

    • 也可以试试——导出默认异步函数apiNewSession;如果您想导出异步函数,它可以工作。
    猜你喜欢
    • 2021-07-13
    • 2021-11-17
    • 2018-04-07
    • 2015-11-24
    • 2021-06-21
    • 2020-06-29
    • 2020-06-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多