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
72702ee2
Commit
72702ee2
authored
5 years ago
by
Mariana Molina
Browse files
Options
Downloads
Patches
Plain Diff
Empiezo obtener usuario por id con base de datos
parent
f1a8bf7a
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
+25
-3
25 additions, 3 deletions
src/database.js
src/routes/user.js
+5
-1
5 additions, 1 deletion
src/routes/user.js
with
30 additions
and
4 deletions
src/database.js
+
25
−
3
View file @
72702ee2
...
...
@@ -24,14 +24,36 @@ exports.addUser = (obj) => {
}
// Obtiene un usuario de la base de datos
exports
.
getUserById
=
(
id
)
=>
{
mongo
.
connect
(
url
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
},
(
err
,
client
)
=>
{
exports
.
getUserById
=
async
(
id
)
=>
{
const
client
=
await
mongo
.
connect
(
url
,
{
useNewUrlParser
:
true
,
useUnifiedTopology
:
true
});
console
.
log
(
'
Base de datos conectada!
'
);
const
db
=
client
.
db
(
dbName
);
const
query
=
{
"
email
"
:
id
};
let
result
;
try
{
result
=
await
db
.
collection
(
userCollectionName
).
findOne
(
query
);
console
.
log
(
result
);
}
catch
(
err
)
{
console
.
error
(
err
);
}
/*
const res = await mongo.connect(url, {useNewUrlParser: true, useUnifiedTopology: true}, async (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
)});
const query = { "email": id };
let result;
try {
result = await db.collection(userCollectionName).findOne(query);
} catch(err) {
console.error(err);
}
console.log(result, '1. Resultado de getUserById antes de retornar');
return result;
});
console.log(res, '2. Lo que devuelve la conexión de mongo');
resolve(res);*/
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/routes/user.js
+
5
−
1
View file @
72702ee2
...
...
@@ -24,17 +24,21 @@ router.get('/users', (req, res) => {
});
// [GET] OBTENER UN USUARIO: /user/:id
router
.
get
(
'
/user/:id
'
,
(
req
,
res
)
=>
{
router
.
get
(
'
/user/:id
'
,
async
(
req
,
res
)
=>
{
try
{
if
(
users
)
{
// Busco el usuario con email = parámetro id del request
const
userEmail
=
req
.
params
.
id
;
const
user
=
await
database
.
getUserById
(
userEmail
);
console
.
log
(
user
,
'
3. Lo que devolvió getUserById
'
);
/*
const user = users.find((data) => data.email === userEmail);
if (!user) {
res.status(500).send('Usuario no encontrado.');
} else {
res.json(user);
}
*/
}
else
{
res
.
status
(
500
).
send
(
'
No se ha agregado ningún usuario aún.
'
);
}
...
...
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