Node.js 模型不是邮递员中的函数错误
我在mongodb中执行基本的crud操作,当我尝试向db插入新的post时,我在post man中得到一条消息,因为测试不是一个函数 我的路由器功能如下Node.js 模型不是邮递员中的函数错误,node.js,mongodb,mongoose,Node.js,Mongodb,Mongoose,我在mongodb中执行基本的crud操作,当我尝试向db插入新的post时,我在post man中得到一条消息,因为测试不是一个函数 我的路由器功能如下 router.route('/createtests').post(function (req, res, next) { var Test = new Test(req.body); postTest(Test, function (data) { res.json({'message': 'The te
router.route('/createtests').post(function (req, res, next) {
var Test = new Test(req.body);
postTest(Test, function (data) {
res.json({'message': 'The test created sucessfully'});
});
});
var postTest = function(test, cb){
Test.save(function(err,data){
cb(data);
});
};
我的模式如下
var TestSchema = common.Schema({
title : String,
testCreator : String,
datePosted : {
type: Date,
default: Date.now
},
totalQuestions : Number,
totalTime : Number,
numberOfPeopleTaking : Number,
dateOfTest : Date,
marksPerQuestions : Number,
imageUrl : String,
testType : String,
});
var Test = common.conn.model('Test', TestSchema);
console.log(typeof Test);// logging as function
console.log(Test);// logging full model with schema
module.exports = Test;
我得到的答复如下
{
"message": "Test is not a function",
"error": {}
}
在函数
postest
中,您使用“t
”进行test
,并使用“t
”进行保存(test.save()
):大写/小写打字。这就是导致您出现问题的原因
var postTest = function(test, cb){
test.save(function(err,data){ //see the change here 'test' instead of 'Test'
cb(data);
});
};
router.route('/createtests').post(function (req, res, next) {
var test = new Test(req.body); //See the change here. 'test' instead of 'Test'
postTest(test, function (data) { //pass 'test'
res.json({'message': 'The test created sucessfully'});
});
});
另外,将common.conn.model
更改为common.model
var Test = common.model('Test', TestSchema);
编辑
您将Test
用作变量
名称和模型
名称。将var更改为test
。它应该能解决你的问题
var postTest = function(test, cb){
test.save(function(err,data){ //see the change here 'test' instead of 'Test'
cb(data);
});
};
router.route('/createtests').post(function (req, res, next) {
var test = new Test(req.body); //See the change here. 'test' instead of 'Test'
postTest(test, function (data) { //pass 'test'
res.json({'message': 'The test created sucessfully'});
});
});
您需要以正确的方式编写代码
const Test = require('../models/Test'); // path
var test = new Test({
email: req.body.title,
password: req.body.testType
});
test.save(function(err,data){
cb(data);
});
我认为您正在传递一个“Test”实例作为参数,但您使用的是
Test
作为实例,而不是Test
你可以试试这个希望,它会起作用的。因为刚刚用虚拟记录测试了它,它工作了,如果它没有意义,意味着你的猫鼬模式有问题
router.route('/createtests').post(function (req, res, next) {
var Test = new Test(req.body);
postTest(Test, function (data) {
res.json({'message': 'The test created sucessfully'});
});
});
var postTest = function(test, cb){
test.save(function(err,data){
if(!err)
cb(null,data);
});
};
是猫鼬的常见参照物吗?是。这是猫鼬的目标。Schema:require('mongoose')。Schema是公共属性。Tryvar Test=common.model('Test',TestSchema)代码>而不是var Test=common.conn.model('Test',TestSchema)代码>不,事实上,它没有接线,这样它将无法工作。。。。。conn:require(mongoose).createconnection(dburl),所以只有我用这种方式连接。。。。。我已经这样做了,甚至要求把月亮鹅放在同一个文件里,但同样的事情,你的终端里有stackTrace吗?那会很有帮助的。还有T和T的拼写问题。我编辑了答案,将common.conn.model
更改为common.model
。你能检查一下这是否有效吗?另外,请告诉我你是如何定义common
的。当您执行console.log(req.body)
req.body
正确记录时,结果是什么?请参见上一个文件,它将测试类型记录为函数。但一旦控件达到var测试=新测试(请求主体)代码>它只是发送一条消息,因为测试不是一个函数,其余的行根本没有执行。请参阅我的编辑,这应该可以解决您的问题。请确认它是否有效。这就是谜团所在。学会了我们不应该同时使用变量对象和函数对象的名称!。。。node并没有我们聪明!谢谢Ravi ji,因为你的做法是错误的。尝试在控制台上打印-{req.body}-您传递的对象类型错误。