最新消息: 电脑我帮您提供丰富的电脑知识,编程学习,软件下载,win7系统下载。

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

IT培训 admin 12浏览 0评论

如何使用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不是函数

我正在尝试运行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 = ...

回答如下:

代替

发布评论

评论列表 (0)

  1. 暂无评论