admin管理员组文章数量:815140
如何使用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从架构中创建模型未捕获的TypeErrorUsercreate不是函数
版权声明:本文标题:如何使用Mongoose从架构中创建模型?未捕获的TypeError:User.create不是函数 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/biancheng/1715211019a822577.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论