尝试连接到MongoDB数据库时承诺{<Pending>}

时间:2020-10-10 15:04:55

标签: node.js mongodb express mongodb-atlas

在学习教程之后,我有了这个:

import {MongoClient} from "mongodb";

const user = "user_001";
const password = "imagineTheRealPassHere";
const cluster = "cluster0.unkpm";

const url = `mongodb+srv://${user}:${password}@${cluster}.mongodb.net/test?retryWrites=true&w=majority`;

export const connectDatabase = async () => {
    const client = await MongoClient.connect(url, {useNewUrlParser: true, useUnifiedTopology: true});

    const db = client.db('main');

    return {
        listings: db.collection('test_listings')
    };
};

这就是Atlas的外观: enter image description here

在连接字符串中注意,我们有 mongodb.net/test 。我不确定“测试”。应该是

<db_name>

所以我认为应该是“主要” ?但是我也尝试过“ main” ,仍然是同样的问题。

无论如何,然后在快递方面,我有这个:

import express, {Application} from 'express';
import {ApolloServer} from "apollo-server-express";
import {typeDefs, resolvers} from "./graphql/index";
import {connectDatabase} from "./database";

const port = 9000;

const mount = async (app: Application) => {
    const db = await connectDatabase();
    const server = new ApolloServer({typeDefs, resolvers, context: () => db});
    server.applyMiddleware({app, path:"/api"});

    app.listen(port);

    console.log('Fraiser is listening.');

    const listings = db.listings.find({}).toArray();
    console.log(listings);
};

mount(express());

但是当我运行应用程序时,没有看到table_listings表的内容,而是说:Promise {}

我认为与我有关的某些事情是错误的,但我是新来的,不确定要看什么。

0 个答案:

没有答案