1

I want to display chatbot and facebook data at the same time. how to display it? because when I try to run in the browser but it does not appear anything. I've tried to look it up on stackoverflow but did not get the right reference

route.js

app.get('/cpanel/facebook', function(req, res) {
        if (req.session.user == null) {
            res.redirect('/cpanel/login');
        }   else {
            CB.getAllRecords( function(e, chatbot) {
                res.render('cpanel/facebook', { udata : req.session.user, chatbot : chatbot });
            });
            FBM.getAllRecords( function(e, facebook) {
                res.render('cpanel/facebook', { udata : req.session.user, facebook : facebook });
            });
        }
    });

facebook.js

var facebook = db.collection('facebook');

exports.addNewFacebook = function(newData, callback) {
    facebook.findOne({accesstoken:newData.accesstoken}, function(e, o) {
        if (o) {
            callback('accesstoken-taken');
        }   else {
            facebook.insert(newData, {safe: true}, callback);
        }
    });
}

exports.getAllRecords = function(callback) {
    facebook.find().toArray(
        function(e, res) {
            if (e) callback(e)
            else callback(null, res)
        }
    );
}

chatbot.js

var chatbot = db.collection('chatbot');

exports.addNewChatBot = function(newData, callback) {
    chatbot.insert(newData, {safe: true}, callback);
}

exports.getAllRecords = function(callback) {
    chatbot.find().toArray(
        function(e, res) {
            if (e) callback(e)
            else callback(null, res)
        }
    );
}
2
  • 1
    Short answer. Use the promises interface instead of the plain callback insterface with your database and then use Promise.all() to track when both requests are done. You will want to switch to promises for managing asynchronous operations because it's way simpler to manage multiple async operations when using promises. Commented Dec 17, 2017 at 4:38
  • do you have a sample code? Commented Dec 17, 2017 at 5:03

1 Answer 1

2

The easier way to manage asynchronous operations in node.js, especially when you have more than one operation that you want to coordinate is to use Promises instead of plain callbacks. And, fortunately, mongodb supports a promise-based interface for all its asynchronous operations now.

So, first change your methods to return a promise instead of taking a callback:

var chatbot = db.collection('chatbot');

exports.getAllRecords = function() {
    return chatbot.find().toArray();
}

var facebook = db.collection('facebook');

exports.getAllRecords = function() {
    return facebook.find().toArray();
}

Then, you can use those promises with Promise.all() to coordinate:

app.get('/cpanel/facebook', function(req, res) {
    if (req.session.user == null) {
        res.redirect('/cpanel/login');
    } else {
        Promise.all([CB.getAllRecords(), FBM.getAllRecords()]).then(results => {
            res.render('cpanel/facebook', { udata : req.session.user, chatbot : results[0], facebook: results[1]});
        }).catch(err => {
            // render some error page here
            res.sendStatus(500);
        });
    }
});

For a call to just a single function that returns a promise, you can use .then():

app.get('/cpanel/facebook', function(req, res) {
    if (req.session.user == null) {
        res.redirect('/cpanel/login');
    } else {
        FBM.getAllRecords().then(results => {
            res.render('cpanel/facebook', { udata : req.session.user, facebook: results});
        }).catch(err => {
            // render some error page here
            res.sendStatus(500);
        });
    }
});
Sign up to request clarification or add additional context in comments.

9 Comments

Thank you for the help. I tried to follow the code you gave, but the error appears: SyntaxError: Unexpected token>
@ArifFirmansyah - On what line of code is that error? Are you sure you copied the code here exactly? What version of node.js are you running?
sorry I wrong copy paste. now can run in browser. i want to ask too, does promise.all it need npm install --save promise-all? because I do that
@ArifFirmansyah - Promise.all() is part of ES6 and is built into recent versions of node.js so it does not need to be separately installed.
I want to ask again, how to display single data? for example just chatbot only
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.