How to multiple fetch data expressJS

104
December 17, 2017, at 4:25 PM

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)
        }
    );
}
Answer 1

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);
        });
    }
});
READ ALSO
Error trying to deploy to firebase due to functions

Error trying to deploy to firebase due to functions

I am getting an error trying to deploy to firebase due to functions

110
Separate Node and React App: Allow Express passthrough to React Router

Separate Node and React App: Allow Express passthrough to React Router

I have two separate apps: one of them is my API in Node with Express, and the other is my front end in React with React Router

88
PDF is uploaded but blank

PDF is uploaded but blank

I'm trying to upload a PDF file to AWS using the request data I'm gettingThis is what the data looks like

84