Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
noSQL
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mariana Molina Reyes
noSQL
Commits
f1a8bf7a
Commit
f1a8bf7a
authored
5 years ago
by
Mariana Molina
Browse files
Options
Downloads
Patches
Plain Diff
Conecto con base de datos mongodb
parent
1472dda5
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/database.js
+34
-30
34 additions, 30 deletions
src/database.js
src/routes/user.js
+2
-0
2 additions, 0 deletions
src/routes/user.js
with
36 additions
and
30 deletions
src/database.js
+
34
−
30
View file @
f1a8bf7a
const
mongoose
=
require
(
'
mongoose
'
);
const
url
=
'
mongodb://localhost:27017/Laboratorio02
'
;
const
mongo
=
require
(
'
mongodb
'
);
mongoose
.
connect
(
url
,
{
useUnifiedTopology
:
true
,
useNewUrlParser
:
true
});
const
db
=
mongoose
.
connection
;
// Connection URL
const
url
=
"
mongodb://localhost:27017
"
;
// Database Name
const
dbName
=
'
Laboratorio02
'
;
const
userCollectionName
=
'
Users
'
;
const
userSchema
=
new
mongoose
.
Schema
({
email
:
{
type
:
String
,
required
:
true
}
});
// Agrega un usuario a la base de datos
exports
.
addUser
=
(
obj
)
=>
{
mongo
.
connect
(
url
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
},
(
err
,
client
)
=>
{
if
(
err
)
{
console
.
log
(
err
);
process
.
exit
(
0
);
}
console
.
log
(
'
Base de datos conectada!
'
);
const
db
=
client
.
db
(
dbName
);
db
.
collection
(
userCollectionName
).
insertOne
(
obj
,
(
err
,
res
)
=>
{
if
(
err
)
throw
err
;
console
.
log
(
'
Usuario agregado a la base de datos.
'
);
client
.
close
();
});
});
}
const
commentSchema
=
new
mongoose
.
Schema
({
id
:
{
type
:
String
,
required
:
true
},
user
:
{
type
:
String
,
required
:
true
},
text
:
{
type
:
Date
,
required
:
true
,
default
:
''
}
});
db
.
on
(
'
error
'
,
(
error
)
=>
console
.
error
(
error
));
db
.
once
(
'
open
'
,
()
=>
{
console
.
log
(
'
Database created!
'
);
});
// Obtiene un usuario de la base de datos
exports
.
getUserById
=
(
id
)
=>
{
mongo
.
connect
(
url
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
},
(
err
,
client
)
=>
{
if
(
err
)
{
console
.
log
(
err
);
process
.
exit
(
0
);
}
console
.
log
(
'
Base de datos conectada!
'
);
const
db
=
client
.
db
(
dbName
);
db
.
collection
(
userCollectionName
).
findOne
({
"
email
"
:
id
}).
toArray
((
err
,
result
)
=>
{
console
.
log
(
result
)});
});
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/routes/user.js
+
2
−
0
View file @
f1a8bf7a
const
express
=
require
(
'
express
'
);
const
bodyParser
=
require
(
'
body-parser
'
);
const
users
=
require
(
'
../models/User
'
).
users
;
const
database
=
require
(
'
../database
'
);
const
app
=
express
();
...
...
@@ -51,6 +52,7 @@ router.post('/user', (req, res) => {
if
(
req
.
body
.
email
)
{
const
newUser
=
{
email
:
req
.
body
.
email
.
toString
(),
comment
:
[]
};
console
.
log
(
newUser
);
database
.
addUser
(
newUser
);
users
.
push
(
newUser
);
res
.
json
(
users
);
}
else
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment