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、openeddbは別の変数であり、データベースを開くときに指定するコールバックへのパラメーターとして指定されています。これは、dbと呼ばれる2つの異なる変数があり、それぞれが異なるスコープで表示され、異なるオブジェクトを参照しているために発生します。画像をわかりやすくするために、関数にパラメーターを追加し、コールバックパラメーターpopulateDBを作成しました。以下の変更が役立つはずです:db1db2

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 に答える