1

I have this issue whit my code, i have a kind of data i need to get current when the user ask for it, i use promise to get the right data out and thats part working well.

my next issue is first time i ask for the data, its return nothing but, if i click agin for the data its return the right data, so something are wrong here.

my promise function

var insert = new Promise(function(fulfill) {
  fulfill('test');
});

my export moduls

exports.signup = function(db, user_conf) {
    var self = this;

    defineUser(user_conf);
    insert.then(function(result) {
        self.json_response = result;
      console.log(result);
    }).catch(function(e) {
      console.log(e);
    });

    return self.json_response;
}

my express route function

router.post('/signup', function(req, res, next) {

    var post = req.body;

    json_response = users.signup(req.db, {
        'fullname' : post["account-fullname"],
        'username' : post["account-username"],
        'email' : post["account-email"],
        'password' : post["account-password"],
        'retype-password' : post["account-retype-password"],
        'accept-terms' : post["accept-terms"]
    });

    res.send(json_response);
});

what i need its i need the respons from my signup part to know about the user can be created or there are a kind of validations error the user need to know before the user can be created.

ParisNakitaKejser
  • 12,112
  • 9
  • 46
  • 66
  • `if i click agin` - there's no evidence that any of the code you posted is on a client that has something to `click` – Jaromanda X Nov 15 '15 at 14:31
  • however, your `export moduls` is incorrect you are returning `self.json_response;` synchronously, however you are assigning a value to it `asynchronously` – Jaromanda X Nov 15 '15 at 14:33
  • i have a button there send post request to signup pages, first time try to signup its not return status codes as i want but second time its dos and its create both users. This code its a simplified and its still not working, its return "test" first second time i run the promise part, its not return test first time. – ParisNakitaKejser Nov 15 '15 at 14:33
  • 1
    your `export moduls` is incorrect you are returning `self.json_response;` synchronously, however you are assigning a value to it `asynchronously` – Jaromanda X Nov 15 '15 at 14:35
  • Its sound right, but how shut i run synchronously for this part of my code? – ParisNakitaKejser Nov 15 '15 at 14:37
  • you can't turn asynchronous code synchronous - well ... maybe some ES7 stuff can make it look that way, but asynchronous is asynchronous, you need to refactor your code to take into consideration the asynchronous nature of the asynchronous parts of the code – Jaromanda X Nov 15 '15 at 14:39

1 Answers1

2

Off the top of my head - try something like this

exports.signup = function(db, user_conf) {
    defineUser(user_conf); // I'm assuming this is synchronous
    return insert.then(function(result) {
        // do something here maybe? if not then you only need to return insert;
        return result; // return a result
    });
}

// if nothing is being done in the then callback above, this can be simplified to

exports.signup = function(db, user_conf) {
    defineUser(user_conf); // I'm assuming this is synchronous
    return insert;
}

router.post('/signup', function(req, res, next) {
    var post = req.body;
    users.signup(req.db, {
        'fullname' : post["account-fullname"],
        'username' : post["account-username"],
        'email' : post["account-email"],
        'password' : post["account-password"],
        'retype-password' : post["account-retype-password"],
        'accept-terms' : post["accept-terms"]
    }).then(function(json_response) {
        res.send(json_response);
    });
});
Jaromanda X
  • 53,868
  • 5
  • 73
  • 87