无法实例化猫鼬架构:"对象不是函数
Cannot instantiate mongoose schema: "Object is not a function"
在我的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);
由于模式无法实例化并用作模型,因此触发了错误。您需要先使用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 */ })
})
相关文章:
- "“;变量未引用正确的对象
- 如何创建js常量对象与"依赖/相对;价值观
- "属性描述必须是对象“”;错误
- 获取"下一个“;对象中的数值
- 得到"TypeError:对象不是函数“”;在forEach循环中使用超级测试/超级代理时
- "键“;jQuery中的对象无法正常工作
- 为什么下面抛出一个“;对象没有't支持属性或方法'importNode'"在IE11中
- "未捕获[对象对象]”;投掷和接球时
- "对象不是函数“;当将Node.js HTTP服务器对象传递给Socket.IO时
- 将对象设置为Backbone.js模型,而不必调用“;set()"在每一处房产上
- 得到"未捕获的类型错误:未定义的不是函数“;当试图从对象调用get()或set()时
- 获得错误“;对象没有方法'jScrollPane'"与AngularJS一起使用时
- "类型错误:'null'不是对象(正在评估“a.nodeType')”;在phanto
- "这个“;对象函数内部的引用
- "对象没有't支持这种性质或方法“;在IE 8中.在调试中执行精细
- "Function.createDelegate"原因;错误:对象没有't支持属性或方法
- 在javascript中,使用requireJS,我如何"要求“;typescript对象
- a href="上的javascript对象对象;javascript:"呼叫
- TypeError:undefined不是一个对象(正在评估'msg.innerHTML="Uploa
- "[对象对象]”;类型为String