【问题标题】:How to send mongodb data to browser using Node.js and eclipse如何使用 Node.js 和 eclipse 将 mongodb 数据发送到浏览器
【发布时间】:2015-04-09 10:57:22
【问题描述】:

我想在浏览器上打印使用 node.js 从 mongodb 获取的数据。数据在 docs 对象中。我想将此数据传递给 ejs 文件。这样我就可以将其插入到表中:

var express = require('express');
var router = express.Router();

/* GET users listing. */

router.get('/', function(req, res, next) {

  res.send('respond with a resource');

  var findDocuments = function(db, callback) {

    // Get the documents collection
    var collection = db.collection('Pages');
    // Find some documents
    collection.find({}).toArray(function(err, docs) {
      assert.equal(err, null);
      //assert.equal(6, docs.length);
        res.render('users', { title: docs });
      console.log("Found the following records");

        docs.forEach(function(doc) {
            console.log("Doc from Array ");
            console.dir(doc.PAGE_NAME);
             });
               callback();
    });

  };

  var MongoClient = require('mongodb').MongoClient

      , assert = require('assert');

  var url = 'mongodb://localhost:27017/test';

  MongoClient.connect(url, function(err, db) {

    assert.equal(null, err);
    console.log("Connected correctly to server");

    findDocuments(db, function() {
      db.close();
    });

  });

});

module.exports = router;

但它抛出了一个错误:

Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
    at ServerResponse.header (c:\Users\ELITEBOOK 8460P\WebstormProjects\untitled1\node_modules\express\lib\response.js:700:10)
    at ServerResponse.res.contentType.res.type (c:\Users\ELITEBOOK 8460P\WebstormProjects\untitled1\node_modules\express\lib\response.js:537:15)
    at ServerResponse.send (c:\Users\ELITEBOOK 8460P\WebstormProjects\untitled1\node_modules\express\lib\response.js:129:14)
    at fn (c:\Users\ELITEBOOK 8460P\WebstormProjects\untitled1\node_modules\express\lib\response.js:934:10)
    at View.exports.renderFile [as engine] (c:\Users\ELITEBOOK 8460P\WebstormProjects\untitled1\node_modules\ejs\lib\ejs.js:353:10)
    at View.render (c:\Users\ELITEBOOK 8460P\WebstormProjects\untitled1\node_modules\express\lib\view.js:93:8)
    at EventEmitter.app.render (c:\Users\ELITEBOOK 8460P\WebstormProjects\untitled1\node_modules\express\lib\application.js:566:10)
    at ServerResponse.res.render (c:\Users\ELITEBOOK 8460P\WebstormProjects\untitled1\node_modules\express\lib\response.js:938:7)
    at c:\Users\ELITEBOOK 8460P\WebstormProjects\untitled1\app.js:54:7

请帮忙....

【问题讨论】:

    标签: javascript eclipse node.js mongodb ejs


    【解决方案1】:

    你发送了两次响应

    1-res.send('respond with a resource');

    2-res.render('users', { title: docs });

    var express = require('express');
    var router = express.Router();
    var MongoClient = require('mongodb').MongoClient,
      assert = require('assert');
    
    
    var url = 'mongodb://localhost:27017/test';
    /* GET users listing. */
    
    router.get('/', function(req, res, next) {
    
      // res.send('respond with a resource'); 
    
    
      MongoClient.connect(url, function(err, db) {
    
        assert.equal(null, err);
        console.log("Connected correctly to server");
    
        var collection = db.collection('Pages');
        // Find some documents
        collection.find({}).toArray(function(err, docs) {
          if (err) {
            throw err;
            return;
          }
    
          console.log("Found the following records");
    
          docs.forEach(function(doc) {
            console.log("Doc from Array ");
            console.dir(doc.PAGE_NAME);
          });
    
          res.render('users', {
            title: docs
          });
          db.close();
        });
    
      });
    
    
    });
    
    module.exports = router;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-12
      • 1970-01-01
      • 2017-08-18
      • 1970-01-01
      • 2016-04-02
      • 2014-07-27
      相关资源
      最近更新 更多