1

当我尝试运行“http://localhost:3000/light”时,在 CURL 中收到错误“来自服务器的空回复”

var express = require('express'),
    values = require('./routes/values');

var app = express();

app.get('/light', values.findAll);

app.listen(3000);
console.log('Listening on port 3000...');

我的 values.js 是

var mongo = require('mongodb');

var Server = mongo.Server,
    Db = mongo.Db,
    BSON = mongo.BSONPure;

var server = new Server('localhost', 27017, {auto_reconnect: true});
db = new Db('sensordb', server);

db.open(function(err, db) {
    if(!err) {
        console.log("Connected to 'sensordb' database");
        db.collection('values', {safe:true}, function(err, collection) {
            if (err) {
                console.log("The 'values' collection doesn't exist. Creating it with sample data...");
                populateDB();
            }
        });
    }
});

exports.findAll = function(req, res) {
    db.collection('values', function(err, collection) {
        collection.find().toArray(function(err, items) {
            res.send(items);
        });
    });
};

var populateDB = function() {

    var values = [
    {
        value: "10",
        date: "121212",
        time: "1214"
    },
    {
        value:  "12",
        date: "121212",
        time: "1224"
    }];

    db.collection('values', function(err, collection) {
        collection.insert(values, {safe:true}, function(err, result) {});
    });

};

基本上在上面的代码中,我创建了一个数据库,如果数据库为空,我尝试填充它。当我在终端上运行服务器代码时,我也会得到类似的东西:

=  Please ensure that you set the default write concern for the database by setting    =
=   one of the options                                                                 =
=                                                                                      =
=     w: (value of > -1 or the string 'majority'), where < 1 means                     =
=        no write acknowlegement                                                       =
=     journal: true/false, wait for flush to journal before acknowlegement             =
=     fsync: true/false, wait for flush to file system before acknowlegement           =
=                                                                                      =
=  For backward compatibility safe is still supported and                              =
=   allows values of [true | false | {j:true} | {w:n, wtimeout:n} | {fsync:true}]      =
=   the default value is false which means the driver receives does not                =
=   return the information of the success/error of the insert/update/remove            =
=                                                                                      =
=   ex: new Db(new Server('localhost', 27017), {safe:false})                           =
=                                                                                      =
=   http://www.mongodb.org/display/DOCS/getLastError+Command                           =
=                                                                                      =
=  The default of no acknowlegement will change in the very near future                =
=                                                                                      =
=  This message will disappear when the default safe is set on the driver Db           =
========================================================================================

上述错误是否引起了一些问题?如果是这样,是什么?如果不是,那有什么责任?

任何帮助将非常感激。

4

1 回答 1

2

似乎您的populateDB函数使用全局db变量,而打开db的是一个不同的变量,作为您在打开数据库时提供的回调的参数给出。这是因为您有两个称为 db 的不同变量,每个变量在不同的范围内可见并引用不同的对象。为了使图片更清晰,我在您的populateDB函数中添加了一个参数,并创建了db1回调db2参数。下面的修改应该会有所帮助:

db.open(function(err, db1) {
...
                populateDB(db1);

...
var populateDB = function(db2) {
...
    db2.collection('values', function(err, collection) {
        collection.insert(values, {safe:true}, function(err, result) {});
    });

};
于 2012-12-10T19:58:22.453 回答