Skip to content

Commit

Permalink
remote mongo
Browse files Browse the repository at this point in the history
  • Loading branch information
David Golodetsky committed May 4, 2020
1 parent 64a9f98 commit e4efc6a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DATABASE_URL = mongodb://localhost/characters
DATABASE_URL = mongodb+srv://davidGo1995:[email protected]/test?retryWrites=true&w=majority
DATABASE_LOCAL = mongodb://localhost/characters
15 changes: 4 additions & 11 deletions routes/games.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,13 @@
const express = require('express')
const Game = require('../models/game');
const router = express.Router();
const mongodb = require('mongodb')

async function getGames() {
const client = await mongodb.MongoClient.connect('mongodb+srv://davidGo1995:secret123#[email protected]/test?retryWrites=true&w=majority',
{ useNewUrlParser: true })
return client.db('Cluster0').collection('games')
}

router.get('/', async (req, res) => {
// user = req.query.user
user = req.query.user
try {
const games = await getGames();
// const userGames = games.filter(game => game.user === user)
res.send(await games.find({}).toArray())
const games = await Game.find();
const userGames = games.filter(game => game.user === user)
res.json(userGames);
} catch (err) {
res.status(500).json({ message: err.message });
}
Expand Down
4 changes: 2 additions & 2 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ const users = require('./routes/users');
const PORT = process.env.PORT || 3000

mongoose.connect(process.env.DATABASE_URL, { useNewUrlParser: true, useFindAndModify: false, useUnifiedTopology: true });
const db = mongoose.connection;

const db = mongoose.connection;
db.on('error', (error) => console.error(error));
db.once('open', () => console.log('Connected to database!'));
db.on('connected', () => console.log('Connected to database!'));

app.use(express.json());
app.use(cors());
Expand Down

0 comments on commit e4efc6a

Please sign in to comment.