Skip to content

Commit

Permalink
Merge pull request #15 from zntb/dev
Browse files Browse the repository at this point in the history
Fix mongodb error
  • Loading branch information
zntb authored Oct 7, 2024
2 parents 0d93767 + cb71aba commit f4d9f23
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,16 @@ jobs:
with:
node-version: '20'

- name: Set up environment variables
run: |
echo "MONGODB_URI=${{ secrets.MONGODB_URI }}" > .env
echo "PORT=3001" >> .env
- name: Install dependencies
run: npm install && cd ./frontend && npm install
run: |
npm install
cd ./frontend && npm install
cd ..
- name: Run linter
run: npm run lint
Expand Down
23 changes: 10 additions & 13 deletions mongo.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
/* eslint-disable no-console */
const mongoose = require('mongoose');
require('dotenv').config();

const password = process.argv[2];
const name = process.argv[3];
const number = process.argv[4];
const name = process.argv[2];
const number = process.argv[3];

const url = `mongodb+srv://zntb:${password}@opencourse.dp4llgl.mongodb.net/phonebook-app?retryWrites=true&w=majority&appName=opencourse`;
const url = process.env.MONGODB_URI;

mongoose.set('strictQuery', false);

mongoose
.connect(url)
.then(() => {
console.log('connected to MongoDB');
console.log('Connected to MongoDB');

if (!name && !number) {
return listAllEntries();
Expand All @@ -24,7 +24,7 @@ mongoose
}
})
.catch(error => {
console.log('error connecting to MongoDB:', error.message);
console.log('Error connecting to MongoDB:', error.message);
});

const personSchema = new mongoose.Schema({
Expand All @@ -35,22 +35,19 @@ const personSchema = new mongoose.Schema({
const Person = mongoose.model('Person', personSchema);

function addEntry(name, number) {
const person = new Person({
name,
number,
});
const person = new Person({ name, number });

return person.save().then(() => {
console.log(`added ${name} number ${number} to phonebook`);
console.log(`Added ${name} number ${number} to phonebook`);
return listAllEntries();
});
}

function listAllEntries() {
return Person.find({}).then(persons => {
console.log('phonebook:');
console.log('Phonebook:');
persons.forEach(person => {
console.log(`${person.name} ${person.number}`);
console.log(`${person.name}: ${person.number}`);
});
mongoose.connection.close();
});
Expand Down

0 comments on commit f4d9f23

Please sign in to comment.