db insert使用factory和angularjs

时间:2014-09-24 11:22:22

标签: angularjs mongoose

我已经创建了角度工厂,我希望在mongoose db中插入数据,但我不知道该怎么做。

我创建的工厂如下:

test.factory('registrationservice', function($http){
    var factory = {};
    console.log('1');
    factory.newregister = function(user, $scope) {
       var newUser = new user({
            username: $scope.uName,
            firstname: $scope.fName,
            lastname: $scope.lName,
            email:$scope.mail,
            password: $scope.newpwd

        });

       console.log(newUser);
        var user = require(['../server.js']);
        console.log('2');

     //  return $http.post('data/registration.js');


    }
    return factory;
});

我有另一个js文件,我已将我的数据库连接定义为:

var mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/userregistration');
console.log('1');
var userSchema = {
    username: String,
    firstname:String,
    lastname: String,
    email: String,
    password: String
}
console.log('2');
var User = mongoose.model('User', userSchema, 'user');
console.log('3');



newUser.save(function(err){
   if(!err) console.log('inserted successfull');
    else
   {
       console.log('failed');
   }

});

任何想法,如何使用工厂实现这一目标?

0 个答案:

没有答案