gpt4 book ai didi

javascript - 无法实例化 Mongoose 模式: "Object is not a function"

转载 作者:太空宇宙 更新时间:2023-11-04 03:08:46 25 4
gpt4 key购买 nike

在我的routes/index.js 文件中,我有:

var mongoose = require('mongoose/');

...

var schema = mongoose.Schema;
var user_details = new schema(
{
username: String,
password: String
},
{
collection: 'userInfo'
});

router.post('/newuser', function(request, response, next)
{
var newuser = new user_details(
{
'username': request.params.username,
'password': request.params.password
});
newuser.save();
response.redirect('/');
});

这给出了以下错误。 48:17 位置是“var newuser = new user_details(”行中的“new”:

object is not a function

TypeError: object is not a function
at module.exports (/Users/jonathan/server/routes/index.js:48:17)
at Layer.handle [as handle_request] (/Users/jonathan/server/node_modules/express/lib/router/layer.js:82:5)
at next (/Users/jonathan/server/node_modules/express/lib/router/route.js:110:13)
at Route.dispatch (/Users/jonathan/server/node_modules/express/lib/router/route.js:91:3)
at Layer.handle [as handle_request] (/Users/jonathan/server/node_modules/express/lib/router/layer.js:82:5)
at /Users/jonathan/server/node_modules/express/lib/router/index.js:267:22
at Function.proto.process_params (/Users/jonathan/server/node_modules/express/lib/router/index.js:321:12)
at next (/Users/jonathan/server/node_modules/express/lib/router/index.js:261:10)
at Function.proto.handle (/Users/jonathan/server/node_modules/express/lib/router/index.js:166:3)
at router (/Users/jonathan/server/node_modules/express/lib/router/index.js:35:12)
at Layer.handle [as handle_request] (/Users/jonathan/server/node_modules/express/lib/router/layer.js:82:5)
at trim_prefix (/Users/jonathan/server/node_modules/express/lib/router/index.js:302:13)
at /Users/jonathan/server/node_modules/express/lib/router/index.js:270:7
at Function.proto.process_params (/Users/jonathan/server/node_modules/express/lib/router/index.js:321:12)
at next (/Users/jonathan/server/node_modules/express/lib/router/index.js:261:10)
at SessionStrategy.strategy.pass (/Users/jonathan/server/node_modules/passport/lib/middleware/authenticate.js:318:9)

我对“对象不是函数”的理解是,某个对象(尝试)被作为函数调用,例如{0: false, 1: true}()。但是你能解释一下我的代码中是什么触发了我的错误吗?

--更新--

我想我正在按照答案第一条评论中的建议进行操作。我现在收到的错误是:

/Users/jonathan/node_modules/mongoose/lib/index.js:340
throw new mongoose.Error.OverwriteModelError(name);
^
OverwriteModelError: Cannot overwrite `userInfo` model once compiled.

触发代码行是:

var user = mongoose.model('userInfo', user_details);

最佳答案

由于架构无法实例化并用作模型,因此正在触发错误。你需要做到a mongoose model首先使用 mongoose.model('DocumentName', document)

例如(我从当前项目中复制了其中的一部分,所以它是 ES6):

// user.js
import mongoose from 'mongoose'

let userSchema = mongoose.Schema({
password: String,
username: String
})

userSchema.methods.setUp = function (username, password) {
this.username = username
this.password = password
return this
}

export let User = mongoose.model('User', userSchema)
export default User

// routes.js
import { User } from './models/user'

router.post('/newuser', function (req, res) {
new User()
// note the `setUp` method in user.js
.setUp(req.params.username, req.params.password)
.save()
// using promises; you can also pass a callback
// `function (err, user)` to save
.then(() => { res.redirect('/') })
.then(null, () => /* handle error */ })
})

关于javascript - 无法实例化 Mongoose 模式: "Object is not a function",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31195073/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com