0

I have a module which i wrote myself. It contains 3 functions which i call as chained promises. The last one does not execute and i cannot figure out why.

The module geodata.js

require('use-strict');
var Promise = require('promise');
var NodeGeocoder = require('node-geocoder');
var mongoose = require('mongoose');
var User = require('../models/user');

var options = {
    provider: 'google',
    httpAdapter: 'https',
    apiKey: 'AIzaSyA4v81WbNOMeRL7p911Mxr6PBZnidX0cIM',
    formatter: null
};

module.exports = {

    //*******************************************************
    // Find user and return his adress (street + town)
    //
    //*******************************************************
    findUser: function(username) {
        return new Promise(function(resolve, reject) {
            User.findOne({
                'username': username
            }, function(err, doc) {
                if (!err) {
                    resolve(doc);
                } else {
                    reject(err);
                }
            });
        });
    },
    //*******************************************************
    // Fetch geodata (latitude + longitude) to users record in
    // the user collection.
    //*******************************************************
    fetchGeoData: function(userObj) {
        return new Promise(function(resolve, reject) {
            var geocoder = NodeGeocoder(options);
            var adress = userObj.street + ' ' + userObj.town;
            geocoder.geocode(adress, function(err, res) {
                if (!err) {
                    var res2 = {
                        'username': userObj.username
                    }
                    console.log(res);
                    resolve([res, res2]);
                } else {
                    reject(err);
                }
            });
        });
    },
    //*******************************************************
    // Fetch geodata (latitude + longitude) to users record in
    // the user collection.
    //*******************************************************
    addGeoData: function(message) {
        return new Promise(function(resolve, reject) {
           User.findOne({
               'username': message.username
           }, function(err, doc) {
                console.log(doc);
                if (err) {
                    console.log(err);
                    reject(err);
                }
                if (!doc) {
                    console.log('User not found.');
                    reject('User not found.');
                }
                doc.longitude = String(message.longitude);
                doc.latitude = String(message.latitude);
                doc.save();
                resolve(doc);
            });
        });
    },

    fixJSON: function(inpJSON) {
        var strung = JSON.stringify(inpJSON);
        obj = strung.substring(1, strung.length - 1);
        return (JSON.parse(obj));
    }

};

.. and here is the calling script:

require('use-strict');

var mongoose = require('mongoose');
var dbConfig = require('./db');
var geodata = require('./lib/geodata.js');

// Connect to DB
mongoose.connect(dbConfig.url, {
    auth: {
        authdb: "admin"
    }
});
mongoose.set('debug', true);

geodata.findUser('jimmy').then(function(result) {
        console.log('findUser() done');
        return geodata.fetchGeoData(result);
    }).then(function(result) {
        console.log('fetchGeoData() done');
        var res1Fixed = geodata.fixJSON(result[0]);
        var params = {
            username: result[1].username,
            longitude: res1Fixed.longitude,
            latitude: res1Fixed.latitude
        }
        console.log(params);
        return geodata.addGeoData(params);
    }).then(function(result) {
        console.log('addGeoData() done');
    });

mongoose.connection.close();

the execution stops after this line:

console.log('fetchGeoData() done');

and i can't figure out why?

Regards Jimmy

J. Plexor
  • 33
  • 4

1 Answers1

0

If you are referring to mongoose.connection.close(); that is actually being run before your console.log('addGeoData() done');

Notice that mongoose.connection.close() is synchronous whereas the mongoose.findUser is asynchronous.

zurfyx
  • 31,043
  • 20
  • 111
  • 145
  • Thanks all of you for clarifying this. Got it working now. – J. Plexor Jan 11 '17 at 20:52
  • Hi @J.Plexor if this or any answer has solved your question please consider accepting it by clicking the check-mark. This indicates to the wider community that you've found a solution and gives some reputation to both the answerer and yourself. There is no obligation to do this. – zurfyx Jan 11 '17 at 21:19