forum

Home / DeveloperSection / Forums / Javascript Scope In Callback

Javascript Scope In Callback

Anonymous User 2594 05-Mar-2013

Hi Mindstickians!

I have a method with a callback inside of nodeJS where I'm trying to set a value in the outer function that can be returned with the result of the data that is being passed in the callback to a mongoose call:

'use strict';

var mongoose = require('mongoose')
    ,Alert = mongoose.model('Alert');

exports.getAllAlerts = function() {
    var result = [];
    Alert.find({}, function(err, alerts) {
        if(err) {
            console.log('exception while fetching alerts');
        }
        if(alerts) {
            result = alerts;
            console.log('result: ' + result);
        }
    });
    return result;
}
How can I set the value of result[] with the value of alerts that's coming back in the mongoose callback?

Thanks in advance


Updated on 05-Mar-2013
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By