无法实例化猫鼬模式: [英] Cannot instantiate mongoose schema: "Object is not a function"

查看:68
本文介绍了无法实例化猫鼬模式:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my routes/index.js file, I have:

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":

This is giving the error below. The 48:17 location is the "new" in the "var newuser = new user_details(" line:

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}().但是您能解释一下我的代码中什么触发了我的错误吗?

My understanding of "object is not a function" is that some object has (attemptedly) been called as a function, such as {0: false, 1: true}(). But can you explain what in my code is triggering my error?

-更新-

我认为我正在做答案的第一条评论中建议的内容.我现在遇到的错误是:

I think I'm doing what was suggested in the answer's first comment. The error I'm getting now is:

/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);

推荐答案

由于无法实例化架构并将其用作模型,因此正在触发错误.您需要先使用mongoose.model('DocumentName', document)使其成为猫鼬模型.

The error is being triggered because a schema cannot be instantiated and used as a model. You need to make it a mongoose model first with mongoose.model('DocumentName', document).

例如(我正在从当前项目中复制部分内容,因此是ES6):

For example (I'm copypasta'ing part of this from a current project, so it's 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 */ })
})

这篇关于无法实例化猫鼬模式:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆