【问题标题】:Cannot instantiate mongoose schema: "Object is not a function"无法实例化猫鼬模式:“对象不是函数”
【发布时间】:2015-09-20 14:28:00
【问题描述】:

在我的 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(”行中的“新”:

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

【问题讨论】:

标签: javascript node.js mongodb express mongoose


【解决方案1】:

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

例如(我正在从当前项目中复制粘贴其中的一部分,所以它是 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 */ })
})

【讨论】:

  • 谢谢。我有数据库中表示的模型,它抛出了一个错误。如何检索 Mongoose 中表示的现有模型或使其兼容?
  • @JonathanHayward 您需要将模式设为模型:var User = mongoose.model('User', user_details)。然后,您可以使用new User() 而不是new user_details(…)
  • @JonathanHayward 你最终解决了你的问题吗?
  • 还没有;我还没有用尽资源,但我选择了 node-localstorage,这对我的读者和我来说应该更熟悉。
  • @JonathanHayward 你能链接到你的代码吗(一个 github gist 什么的);从您的更新中,很难判断可能导致错误的原因
猜你喜欢
  • 1970-01-01
  • 2012-01-14
  • 2016-04-22
  • 2019-10-30
  • 2015-06-11
  • 2021-04-10
  • 2020-03-10
  • 2020-03-18
  • 2023-01-28
相关资源
最近更新 更多