如何使用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>> User
<font color="#555753">{ [Function: require] resolve: { [Function: resolve] paths: [Function: paths] }, main: undefined, extensions: [Object: null prototype] { '.js': [Function (anonymous)], '.json': [Function (anonymous)],</font></pre>
> typeof(User)
'function'
我正在尝试运行J. Wexler的书中的代码。该书可在GitHub上找到。现在,我正在研究18_1,第4章。我已按照本书中的所有步骤在REPL中创建用户。 const User = ...
回答如下:代替
更多推荐
如何使用Mongoose从架构中创建模型?未捕获的TypeError:User.create不是函数
发布评论