猫鼬“创建”方法 - 没有回调吗? (使用护照和角度)

时间:2021-09-24 02:35:16

I am using mongoose to create a user object on register. This works fine and any errors are returned as expected.

我正在使用mongoose在寄存器上创建用户对象。这工作正常,任何错误都按预期返回。

However, I want to log the user on right after they register (so registering logs you on if there are no errors).

但是,我想在注册后立即记录用户(如果没有错误,请注册登录)。

I have the following for the register.

我有以下注册表。

register_controller:

register_controller:

$scope.submitRegister = function() {
    AuthenticationService.register(this.details).success(function() {
        $log.debug('/POST to /api/register worked');
    });
}

services.js:

services.js:

.service('AuthenticationService', function($http, $timeout, $q, $session, $flash) {
    ...
    this.register = function(details) {
        var register = $http.post('/api/register', details);
        register.success(function() {
            console.log("User added fine"); 
        }).error(function() {
            console.log("error!!!");
        });
        return register;
    };
    ...

users.js:

users.js:

app.post('/api/register', authentication.register);

passport's authenticate.js:

护照的authenticate.js:

module.exports = {
...
  register: function(req, res){
    var User = require('./controllers/api/login_api');
    User.create({name: req.body.name, email: req.body.email, password: req.body.password}, function(err){
      if (err) {
        console.log(err);
        return;
      }
      console.log("User added"); 
      return res.send(200);
    });
  },
...

The error is reported back fine, no troubles, but I would have thought it would report something else back (like the created object?) which I could use down the line so my register_controller can have in the success function(object) {... login(object);...}.

错误被报告回来没问题,但是我会认为它会报告其他东西(比如创建的对象?),我可以使用它,所以我的register_controller可以在成功函数(对象){.. .login(object); ...}。

Is this a limitation in the .create method or am I missing something obvious?

这是.create方法的限制还是我遗漏了一些明显的东西?

Thank you.

谢谢。

1 个解决方案

#1


3  

Two things to change in your server code:

要在服务器代码中更改两件事:

passport's authenticate.js:

护照的authenticate.js:

module.exports = {
...
  register: function(req, res){
    var User = require('./controllers/api/login_api');
    User.create({name: req.body.name, email: req.body.email, password: req.body.password}, function(err, user){
      if (err) {
        console.log(err);
        return;
      }
      console.log("User added"); 
      return res.send(200, user);
    });
  },
...

I've added user to your callback Mongoose model.create api - return the created object to the CB

我已将用户添加到您的回调Mongoose model.create api - 将创建的对象返回给CB

And to change the catch in your client:

并改变客户端的捕获量:

.service('AuthenticationService', function($http, $timeout, $q, $session, $flash) {
...
this.register = function(details) {
    var register = $http.post('/api/register', details);
    register.success(function(user) {
        console.log(user); 
    }).error(function() {
        console.log("error!!!");
    });
    return register;
};
...

Now you can do with the created user object whatever you need

现在,无论您需要什么,都可以使用创建的用户对象

#1


3  

Two things to change in your server code:

要在服务器代码中更改两件事:

passport's authenticate.js:

护照的authenticate.js:

module.exports = {
...
  register: function(req, res){
    var User = require('./controllers/api/login_api');
    User.create({name: req.body.name, email: req.body.email, password: req.body.password}, function(err, user){
      if (err) {
        console.log(err);
        return;
      }
      console.log("User added"); 
      return res.send(200, user);
    });
  },
...

I've added user to your callback Mongoose model.create api - return the created object to the CB

我已将用户添加到您的回调Mongoose model.create api - 将创建的对象返回给CB

And to change the catch in your client:

并改变客户端的捕获量:

.service('AuthenticationService', function($http, $timeout, $q, $session, $flash) {
...
this.register = function(details) {
    var register = $http.post('/api/register', details);
    register.success(function(user) {
        console.log(user); 
    }).error(function() {
        console.log("error!!!");
    });
    return register;
};
...

Now you can do with the created user object whatever you need

现在,无论您需要什么,都可以使用创建的用户对象