0
我有下面的代碼,我試圖從MongoDb數據庫中獲取文檔並顯示每個文檔的名字屬性。出於某種原因,我得到以下錯誤:訪問ExpressJS App中的MongoDB文檔屬性
TypeError: Cannot read property 'firstName' of undefined
這裏是我的app.js實現:
const express = require('express')
const app = express()
var MongoClient = require('mongodb').MongoClient;
var assert = require('assert');
var db = {}
var url = 'mongodb://localhost:27017/bank';
MongoClient.connect(url, function(err, db) {
assert.equal(null, err);
this.db = db;
console.log("Connected correctly to server.");
db.close();
});
app.get('/customers',function(req,res){
console.log("customers")
this.db.open()
var documents = this.db.collection("customers").find()
documents[0].firstName // how to access the first name property
this.db.close()
res.send("fetching customers")
})
打印文件? –