如何使用Mongoose从架构中创建模型?未捕获的TypeError:User.create不是函数

编程入门 行业动态 更新时间:2024-10-10 12:26:47

<a href=https://www.elefans.com/category/jswz/34/1771452.html style=如何使用Mongoose从架构中创建模型?未捕获的TypeError:User.create不是函数"/>

如何使用Mongoose从架构中创建模型?未捕获的TypeError:User.create不是函数

我正在尝试运行J. Wexler的书中的代码。这本书在GitHub。

现在,我正在研究18_1,第4章。

我已按照本书中的所有步骤在REPL中创建User。>

const User = require
("./models/user")

user.js

"use strict";

const mongoose = require("mongoose"),
  { Schema } = mongoose,
  userSchema = new Schema(
    {
      name: {
        first: {
          type: String,
          trim: true
        },
        last: {
          type: String,
          trim: true
        }
      },
      email: {
        type: String,
        required: true,
        lowercase: true,
        unique: true
      },
      zipCode: {
        type: Number,
        min: [1000, "Zip code too short"],
        max: 99999
      },
      password: {
        type: String,
        required: true
      },
      courses: [{ type: Schema.Types.ObjectId, ref: "Course" }],
      subscribedAccount: {
        type: Schema.Types.ObjectId,
        ref: "Subscriber"
      }
    },
    {
      timestamps: true
    }
  );

userSchema.virtual("fullName").get(function() {
  return `${this.name.first} ${this.name.last}`;
});
module.exports = mongoose.model("User", userSchema);

最后一行输出mongoose.model

反正

User.create({name: { first: "John" ,last : "Walker"}, email: "[email protected]", password : "piha111"}).
... then(user => firstUser = user).
... catch(error => console.log(error.message));

导致此错误

Uncaught TypeError: User.create is not a function

Althouglh节点代表,将用户视为功能

<pre>&gt; User
<font color="#555753">{ [Function: require] resolve: { [Function: resolve] paths: [Function: paths] }, main: undefined, extensions: [Object: null prototype] { &apos;.js&apos;: [Function (anonymous)], &apos;.json&apos;: [Function (anonymous)],</font></pre> 

> typeof(User)
'function'

我正在尝试运行J. Wexler的书中的代码。该书可在GitHub上找到。现在,我正在研究18_1,第4章。我已按照本书中的所有步骤在REPL中创建用户。 const User = ...

回答如下:

代替

更多推荐

如何使用Mongoose从架构中创建模型?未捕获的TypeError:User.create不是函数

本文发布于:2024-05-07 14:10:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1756618.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   架构   函数   模型   Mongoose

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!