NodeJs,AngularJs,Mongoose给出错误:TypeError:Object.keys在Function.keys(native)上的非对象上调用

时间:2015-11-11 16:02:06

标签: javascript angularjs node.js mongodb mongoose

以下是我的代码相对于问题: server.js文件:

app.post('/user', function(req,res){
    console.log(req.body);
    var user = mongoose.Schema('User',req.body);
    user.save(function(err,user){
        if(err) console.log(err);
        console.log("Successfully added user to MongoDB");
    });
    res.sendStatus(200);
});

角度控制器:

app.controller('RegisterController',['$scope','$http',function($scope,$http){
    $scope.submitData = function(user){
        var dob = user.dob.day.toString()+"/"+user.dob.month.toString()+"/"+user.dob.year.toString();
        user.dob = dob;
        var post = $http.post('/user', user);
        post.success(function(data,status,headers,config){
                console.log(data);
                console.log(status);
                console.log('Success!');
            });  
    }
    $scope.countTo= function(count){
        var array = [];
        for(i = 1; i<=count; i++){
            array.push(i);
        }
        return array;
    }
    $scope.countFrom = function(startingPoint,count){
        var numbers = [];
        for(i=startingPoint; i>startingPoint-count;i--){
            numbers.push(i);
        }
        return numbers;
    }
}

Mongoose Schema:

    var mongoose = require('mongoose');

var userSchema = mongoose.Schema({
    email: { 
        type:String,
        required: true
    },
    dob: {
        type: String,
        required: true
    },
    password: {
        type:String,
        required:true
    },
    postcode: {
        type:String,
        required:true
    },
    address: {
        type:String,
        required:true
    }
});

var User = mongoose.model('User',userSchema);

这是错误:

SyntaxError: Unexpected token o
    at Object.parse (native)
    at P:\learning\Blink\server.js:35:41
    at Layer.handle [as handle_request] (P:\learning\Blink\node_modules\express\lib\router\layer.js:95:5)
    at next (P:\learning\Blink\node_modules\express\lib\router\route.js:131:13)
    at Route.dispatch (P:\learning\Blink\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (P:\learning\Blink\node_modules\express\lib\router\layer.js:95:5)
    at P:\learning\Blink\node_modules\express\lib\router\index.js:277:22
    at Function.process_params (P:\learning\Blink\node_modules\express\lib\router\index.js:330:12)
    at next (P:\learning\Blink\node_modules\express\lib\router\index.js:271:10)
    at serveStatic (P:\learning\Blink\node_modules\express\node_modules\serve-static\index.js:74:16)
    at Layer.handle [as handle_request] (P:\learning\Blink\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (P:\learning\Blink\node_modules\express\lib\router\index.js:312:13)
    at P:\learning\Blink\node_modules\express\lib\router\index.js:280:7
    at Function.process_params (P:\learning\Blink\node_modules\express\lib\router\index.js:330:12)
    at next (P:\learning\Blink\node_modules\express\lib\router\index.js:271:10)
    at P:\learning\Blink\node_modules\body-parser\lib\read.js:129:5

当我点击网站上的发送按钮时发生错误...请帮忙吗?

1 个答案:

答案 0 :(得分:1)

看起来你在做Mongoose方面的事情有点不对劲。不是100%确定这是导致错误的原因,但它仍然存在问题。

var user = mongoose.Schema('User',req.body);

这不是你认为它做的事情。 Schema期望将表示Schema的对象作为第一个参数,例如{email:String,dob:String}。传递字符串&#39; User&#39;这里行不通。

相反,您想要引用您已定义的模型。您可以通过

完成此操作
  • 导出并随后将您的用户模型包括为var User = require('UserModel.js')
  • 等模块
  • 或直接引用Mongoose中的模型(假设它在您调用之前在应用程序中定义),如var User = mongoose.model('User);

一旦有了Model对象的引用,就可以初始化一个新文档。在您的情况下,您可能希望您的代码读取类似

的内容

var user = new User(req.body); user.save(function(err,user){...}

尝试,然后告诉我们您是否仍然看到该错误。