当前位置: 动力学知识库 > 问答 > 编程问答 >

node.js - Mongoose query on NodeJS server returns NULL

问题描述:

I'm querying Twitter for locations, which is successfully returned. These locations are then stored in a mongodb. When I query the database via command line for the location: 'United Kingdom', the location data is returned. However when I query the database on the NodeJS server, nothing is returned (null).

Here is the code:

'use strict';

// Set default node environment to development

process.env.NODE_ENV = process.env.NODE_ENV || 'development';

var express = require('express');

var mongoose = require('mongoose');

var config = require('./config/environment');

var Location = require('./api/twitter/location/twitter/location.model');

// Connect to database

mongoose.connect(config.mongo.uri, config.mongo.options);

mongoose.connection.on('error', function(err) {

console.error('MongoDB connection error: ' + err);

process.exit(-1);

});

// Populate DB with sample data

if(config.seedDB) {

Location.remove({}, function(err) {

console.log('Location collection removed');

});

}

// Setup server

var app = express();

var server = require('http').createServer(app);

var socketio = require('socket.io')(server, {

serveClient: config.env !== 'production',

path: '/socket.io-client'

});

require('./config/socketio')(socketio);

require('./config/express')(app);

require('./routes')(app);

// Start server

server.listen(config.port, config.ip, function () {

console.log('Express server listening on %d, in %s mode', config.port, app.get('env'));

});

// Expose app

exports = module.exports = app;

// Query Twitter for locations

getLocations();

// Find location 'United Kingdom'

var woeId = Location.findOne({'name': 'United Kingdom'}, 'woeId', function (err, location) {

console.log(location);

if (!err) {

return location;

} else {

console.log(err);

}

});

// Gather available locations from Twitter

function getLocations() {

twitterClient.get('trends/available', {}, function(errors, locations, response) {

if (!errors) {

storeLocations(locations);

} else {

console.log(errors);

}

});

}

// Store locations in database

function storeLocations(locations) {

for (var location in locations) {

Location.create({

name: locations[location].name,

placeType: {

code: locations[location].placeType.code,

name: locations[location].placeType.name

},

parentId: locations[location].parentid,

country: locations[location].country,

woeId: locations[location].woeid,

countryCode: locations[location].countryCode

}, function(err) {

if (err) {

console.log(err);

}

});

}

}

Any help with this would be much appreciated and thanks in advance.

网友答案:

In node.js your call to findOne is asynchronous.

// Find location 'United Kingdom'
var woeId = Location.findOne({'name': 'United Kingdom'}, 'woeId', function (err, location) {
    console.log(location);
    if (!err) {
        return location;
    } else {
        console.log(err);
    }
});

It looks like you expect the return value (location) that you provide from the callback to be propogated out to var woeId, but that will never happen.

Instead you need to perform whatever action is needed from within the callback, which could be as simple as setting a global variable, but depends on how you plan on using it. For example:

// Find location 'United Kingdom'
var woeId;
Location.findOne({'name': 'United Kingdom'}, 'woeId', function (err, location) {
        console.log(location);
        if (!err) {
                woeId = location;
        } else {
                console.log(err);
        }
});

But just remember that the value won't be available until the asynchronous callback is invoked.

分享给朋友:
您可能感兴趣的文章:
随机阅读: