节点JS - 函数未执行

时间:2017-01-10 21:39:59

标签: node.js promise

我有一个自己写的模块。它包含3个函数,我称之为链式承诺。最后一个没有执行,我无法弄清楚原因。

模块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));
    }

};




..这是调用脚本:



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();




执行在此行之后停止:

console.log(' fetchGeoData()完成');

我无法找出原因?

此致 麦

1 个答案:

答案 0 :(得分:0)

如果您指的是在mongoose.connection.close();

之前实际运行的console.log('addGeoData() done');

请注意mongoose.connection.close()是同步的,而mongoose.findUser是异步的。