简体   繁体   English

Angular-Fullstack创建后获取user_id

[英]Angular-Fullstack get user_id after create it

I am creating users and need to get the id of the recently created user, but I'm not able to get it through the callback. 我正在创建用户,需要获取最近创建的用户的ID,但是无法通过回调获取它。 This is what I have: 这就是我所拥有的:

controller.js: controller.js:

Auth.createUser({
        name: this.user.name,
        email: this.user.email,
        password: this.user.password,
      }, function (err, user) {
        console.log(user); // Return only the params 'name', 'email, 'password', not the '_id'
}

auth.service.js: auth.service.js:

/**
     * Create a new user
     *
     * @param  {Object}   user     - user info
     * @param  {Function} callback - function(error, user)
     * @return {Promise}
     */
    createUser(user, callback) {
      return User.save(user, function(data) {
        $cookies.put('token', data.token);
        currentUser = User.get();
        return safeCb(callback)(null, user);
      }, function(err) {
        Auth.logout();
        return safeCb(callback)(err);
      })
        .$promise;
    },

EDIT: If I try through the API end point: 编辑:如果我尝试通过API端点:

$http.post('/api/users/', {
        name: this.usr.name,
        email: this.usr.email,
        password: this.usr.password
      })

I get this object: 我得到这个对象:

Object { data: Object, status: 200, headers: headersGetter/<(), config: Object, statusText: "OK" }

Where: 哪里:

data {
    token: eyJhbGciOiJ...Vs3Ng14ycQA
    __proto__: Object 
}

So I cannot get the user info anyway. 所以我还是无法获得用户信息。

Could you help me, guys?? 你们能帮我吗? Thanks in advance. 提前致谢。

Why you are making your task more complicated I think you have misunderstood some concept. 为什么您使您的任务变得更复杂,我认为您误解了一些概念。 Let me try to clear it. 让我尝试清除它。

ok so whenever you have promise to return you get the option resolve and reject that means when either resolve or reject gets called you can get it in then...or...catch call. 好的,所以只要您答应返回就可以得到“解决并拒绝”选项,这意味着当调用“解决”或“拒绝”时,您可以在...或... catch调用中获取它。

Auth.createUser({
        name: this.user.name,
        email: this.user.email,
        password: this.user.password,
      }, function (err, user) {
        console.log(user); // Return only the params 'name', 'email, 'password', not the '_id'
}

Your createUser signature should be 您的createUser签名应为

function createUser(OBJECT,CALLBACK_FUNCTION) 函数createUser(OBJECT,CALLBACK_FUNCTION)

here callback function means that function will be called whenever you want, if you call that function then only you can call this function --> 这里的回调函数意味着将在需要时调用该函数,如果您调用该函数,则只有您可以调用此函数->

function (err, user) { console.log(user); 功能(错误,用户){console.log(user); // Return only the params 'name', 'email, 'password', not the '_id' } //仅返回参数'name','email,'password',而不返回'_id'}

here you are not handling promise you are making a function call which has callback as parameter so you have to write that function like this.. and call the callback accordingly 在这里,您没有处理诺言,您正在进行将回调作为参数的函数调用,因此您必须像这样编写该函数..并相应地调用回调

  createUser(user, callback) {
  User.save(user, function(data) {
    $cookies.put('token', data.token);
    currentUser = User.get();
    //return safeCb(callback)(null, user);
    //call that function callback with needed parameter.
    callback(null,user);//giving first para as null as no error
  }, function(err) {
    Auth.logout();
    //return safeCb(callback)(err);
    callback(err);
  })
},

Hopefully this will help. 希望这会有所帮助。 Let me know if you still have any doubt. 让我知道您是否还有任何疑问。 Edited 编辑

May be you missed a bracket.. 可能是您错过了括号。

 Auth.createUser({
         name: this.user.name,
         email: this.user.email,
         password: this.user.password,
       }, function (err, user) {
         console.log(user); // Return only the params 'name', 'email, 'password', not the '_id' 
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM