Harpreet Singh
Harpreet Singh

Reputation: 979

using return value to send to ajax success in express

i need to return a value from included file to use it in ajax success from express. here is my code for app.js.

var userdetail = {
    "useremail":req.body.useremail,
    "fname" : req.body.fname,
    "lname" : req.body.lname,
    "password" : randompassword
}
var pp = require('./routes/registration.js')(userdetail);

console.log(pp);

And here is my registration.js code

var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true })); 

var UserReg = require('./regschema.js');

module.exports = function(userdetail){

    //console.log("sdsadsa");

    UserReg.find({Email : userdetail.useremail}, function (err, docs) {
        if (docs.length){
            //console.log("present");
            //return("Email Present ");
            //return 'Name exists already';
             pres = "present";
        } else {
            var UserAdd = new UserReg({
                FirstName: userdetail.fname,
                LastName: userdetail.lname,
                Email: userdetail.useremail,
                Pass: userdetail.password
            });

            // Save
            UserAdd.save(function (err, fluffy) {
                if (err) return console.error(err);
            });

            pres = "absent";

            //res.send("Done");
        }
        console.log(pres);
        return pres;
    });
}

What I am looking for is console.log on app.js must show a value either present or absent so i can use that value to send it to ajax success function.

Upvotes: 0

Views: 154

Answers (1)

user5964970
user5964970

Reputation:

You try to mix synchronous and asynchronous function calls which is tricky to mix. The codeblock

var pp = require('./routes/registration.js')(userdetail);
console.log(pp);

runs synchronously, line after line. But in your registration.js file you have the statement

UserReg.find({Email : userdetail.useremail}, function (err, docs) {

which seems to be an asynchronous function which expects a callback function as second parameter. Asynchronous functions do not return their results immediately but sometimes in the future. So you need to wait for the results. You can do this in your main code with also using a callback function like this:

var registration = require('./routes/registration.js');
registration(userdetail, function(pp) {
    console.log(pp);
});

The registration.js file would then be changed in this way to accept a callback function itself:

module.exports = function(userdetail, callback){
    UserReg.find({Email : userdetail.useremail}, function (err, docs) {
        if (docs.length){
            callback('Name exists already');
        } else {
            var UserAdd = new UserReg({
                FirstName: userdetail.fname,
                LastName: userdetail.lname,
                Email: userdetail.useremail,
                Pass: userdetail.password
            });
            // Save
            UserAdd.save(function (err, fluffy) {
                if (err) {
                    callback(err);
                } else {
                    callback('Done');
                }
            });
        }
    });
}

Please pay attention to the UserAdd.save function call which also behaves asynchronously. With the solution above you can handle this encapsulated asynchronous call also.

Upvotes: 1

Related Questions