查看mongodb集合时,MEAN堆栈未加载页面

时间:2018-09-12 23:48:58

标签: angularjs mongodb mean-stack

我正在玩MEAN堆栈,以便自学一些javascript,并使用Angular,并且在设置MEAN环境时遇到了困难。

我正在尝试连接到Mongodb数据库,并读取“用户”集合的内容,该集合应包含两条记录“ John Doe”和“ Jane Doe”。

这是我的连接字符串以及api.js顶部的常量的样子

//Importing express, Router, Mongoclient and ObjectID
const express = require('express');
const router = express.Router();
const MongoClient = require('mongodb').MongoClient;
const ObjectID = require('mongodb').ObjectID;

//Connect
const connection = (closure) => {
    //Connecting to "mongod://localhost" line, "/mean" is the name of the DB you
    //want to connect too
    return MongoClient.connect('mongodb://localhost:27017/mean', { useNewUrlParser: true }, (err, client) => { 
        if (err) return console.log(err);

        var db = client.db('mean');

        db.collection('users').findOne({}, function (findErr, result) {
            if (findErr) throw findErr;
            console.log(result.name);
            client.close()
        });

        //closure(db);
    });
};

当我从命令行运行“节点服务器”时,它运行正常,没有错误:

C:\Users\jack_\OneDrive\Documents\code\mean>node server
Running on localhost:3000

但是,当我尝试导航到“ localhost:3000 / api / users”时,什么也没发生,页面卡住了,如果我回到命令行,它会在“用户”中显示第一个条目:

C:\Users\jack_\OneDrive\Documents\code\mean>node server
Running on localhost:3000
John Doe

我可以毫无问题地打开“ localhost:3000 / api”,这将我带到AngularJS登陆页面。

我已经阅读了overstackoverflow问题,发现mongodb驱动程序可能存在问题,因为他们不小心将beta版本发布到了npm,所以我检查了我的数据库版本和驱动程序版本:

C:\Users\jack_\OneDrive\Documents\code\mean>"C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe" --version
db version v4.0.2
git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
allocator: tcmalloc
modules: none
build environment:
    distmod: 2008plus-ssl
    distarch: x86_64
    target_arch: x86_64

数据库版本:

C:\Users\jack_\OneDrive\Documents\code\mean>"C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe" --version
MongoDB shell version v4.0.2
git version: fc1573ba18aee42f97a3bb13b67af7d837826b47
allocator: tcmalloc
modules: none
build environment:
    distmod: 2008plus-ssl
    distarch: x86_64
    target_arch: x86_64

由于我使用的版本,请查看其他stackoverflow答案,现在MongoClient.connect returns a client object containing the database object

所以我用以下代码修改了代码:

MongoClient.connect('mongodb://localhost:27017/mean', { useNewUrlParser: true }

var db = client.db('mean');

        db.collection('users').findOne({}, function (findErr, result) {
            if (findErr) throw findErr;
            console.log(result.name);
            client.close()

这摆脱了我的原始错误,即“ db.collections不是一个函数”,但是根据我所遵循的教程,我应该在网络浏览器中看到“ users”集合的内容,加上一些其他信息,像这样:

{"status":200,"data":[{"_id":"598ce66bf7d6d70def3d9f6f","name":"John Doe"}

但是,我不是, 那我做错了什么?还是我错过了一些东西,请记住我是在凌晨3点写的,所以我很可能错过了一些东西

预先感谢

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码与db连接并查找数据

Class DetailViewController: UIViewController: {

var selectedStory : URL!
    var selectedIndex: Int!
    var stories: [URL] = []
@IBOutlet weak var lastLabel: UIBarButtonItem!
@IBOutlet weak var nextLabel: UIBarButtonItem!

//MARK: - Tab Bar Next/Last Toolbar button actions
@IBAction func lastButton(_ sender: Any) {
    if ((self.selectedIndex - 1) < self.stories.count) {
        self.selectedIndex = self.selectedIndex - 1
        let nextStory = self.stories[self.selectedIndex]
        DispatchQueue.global().async {
            let nextStoryText = try? String(contentsOf: nextStory)
            DispatchQueue.main.async {
                self.textView.text = nextStoryText
            }
        }
        let storyTitle = nextStory.deletingPathExtension().lastPathComponent
        storyTitleLabel.title = storyTitle
    }

    if selectedIndex == 0 {
        lastLabel.title = ""
    }
        else {
        lastLabel.title = "Last"

    }
}

@IBAction func nextButton(_ sender: Any) {
    if ((self.selectedIndex + 1) < self.stories.count) {
        self.selectedIndex = self.selectedIndex + 1
        let nextStory = self.stories[self.selectedIndex]
        DispatchQueue.global().async {
            let nextStoryText = try? String(contentsOf: nextStory)
            DispatchQueue.main.async {
                self.textView.text = nextStoryText
            }
        }
        let storyTitle = nextStory.deletingPathExtension().lastPathComponent
        storyTitleLabel.title = storyTitle     
    }
    if ((self.selectedIndex + 1) == self.stories.count) {
        nextLabel.title = ""
    }
        else {
        nextLabel.title = "Next"

    }
}
}

class TableViewController: UITableViewController {

var stories: [URL] = []

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if let vc = storyboard?.instantiateViewController(withIdentifier: "DetailVC") as? DetailViewController {
            vc.selectedStory = stories[indexPath.row]
            vc.stories = stories
            vc.selectedIndex = indexPath.row

            navigationController?.pushViewController(vc, animated: true)
        }
    }

}