【问题标题】:.create() on mongoose schema erroring with "this expression is not callable" - TypeScript.create() on mongoose 模式错误,“此表达式不可调用”-TypeScript
【发布时间】:2021-04-27 22:05:48
【问题描述】:

我在猫鼬模型上的 .create 出错了,我无法找出问题所在。我正在使用 Next JS,并且调用发生在 API 路由中......

get 请求工作正常...

pages>API>task.tsx

import dbConnect from "../../util/dbconnect";
import Task from "../../models/Task";

dbConnect();

export default async function (req, res) {
  const { method } = req;
  switch (method) {
    case "GET":
      try {
        const tasks = await Task.find({});
        res.status(200).json({ success: true, data: tasks });
      } catch (error) {
        res.status(400).json({ success: false });
      }
      break;
    case "POST":
      try {
        const task = await Task.create(req.body);
        res.status(201).json({ success: true, data: task });
      } catch (error) {
        res.status(400).json({ success: false });
      }
      break;
    default:
      res.status(400).json({ success: false });
      break;
  }
}

错误信息显示

"这个表达式是不可调用的。 联合类型的每个成员 '{ ,“_id” | ... 6 更多... | “状态”>>>(文档:DocContents):承诺<...>; >(docs: DocContents[], opt...' 有签名,但这些签名都不兼容。ts(2349) "

架构在这里: 模型>Task.tsx

import mongoose, { Schema, Document, model, models } from "mongoose";

export interface ITask extends Document {
  title: string;
  description: string;
  createdDate: string;
  estimatedDueDate?: string;
  status: string[];
}

const TaskSchema: Schema = new Schema({
  title: {
    type: String,
    required: [true, "Please add a title"],
    unique: true,
    trim: true,
    maxlength: [60, "Title cannot be more than 60 Characters "],
  },
  description: {
    type: String,
    required: [true, "Please add a title"],
    unique: true,
    trim: true,
    maxlength: [400, "Title cannot be more than 60 Characters"],
  },
  createdDate: {
    type: Date,
    required: [true],
  },
  estimatedDueDate: {
    type: Date,
    required: [
      false,
      "Entering a due date helps to create your visual timeline",
    ],
  },
  status: {
    type: String,
    required: [true],
    default: "New",
  },
});

export default models.Task || model<ITask>("Task", TaskSchema);

我尝试将 .create() 更改为 await new Task(req.body) - 如果我将 req.body 排除在外,那么该帖子将使用一个空的新文档(其中没有所有Schema 中指定的属性)如果我将 req.body 留在函数调用中,则会出错。

repo 在这里:https://github.com/jondhill333/ProjectManagementTool

感谢您的帮助!

【问题讨论】:

    标签: reactjs mongodb typescript mongoose next.js


    【解决方案1】:

    已修复...发布请求需要更新如下:

     case "POST":
          try {
            const task = await new Task(req.body);
            res.status(201).json({ success: true, data: task });
            task.save();
          } catch (error) {
            res
              .status(400)
              .json({ success: false + " post", message: error.message, error });
          }
    

    【讨论】:

    • ok.. ish 所以问题仍然存在于 Task.findByIdAndUpdate() - 与以前相同的错误消息 - 显然有一些关于 Typescript 接口的东西,我不知道哪个是问题的根源。这很奇怪,因为我在我一直在寻找 Typescript 的任何教程中都没有看到这个错误 - 这两个函数已经能够使用,具有完全相同的接口/模式格式。如果您知道答案,请告诉我,一旦我找到它,我会在这里发布。谢谢!
    猜你喜欢
    • 2021-06-12
    • 2020-08-16
    • 2021-02-12
    • 1970-01-01
    • 2022-09-30
    • 2021-05-05
    • 1970-01-01
    • 2021-05-17
    • 1970-01-01
    相关资源
    最近更新 更多