Search for an element in a MongoDB database using Nodejs

0

Alright, here I have a code where I have my Express server, the connection to the database and apparently everything works fine.

const express = require('express');
const path = require('path');
const cookieParser = require('cookie-parser');
const logger = require('morgan');
const helmet = require('helmet');
const mongoose = require('mongoose');

mongoose.connect('mongodb://localhost/firulais', { useNewUrlParser: true }, ()=> {
  console.log("Connected")
});

//Models
var Users = mongoose.model('Users', {
  id:String,
  first:String,
  last:String,
  age:Number,
  pass:String,
  country:String,
  tel:Number,
  adm:Boolean
})


var Polls = mongoose.model('Polls', {
  id:String,
  name:String,
  description:String,
  yes:Number,
  no:Number,
  veto:Boolean
})

var a = Users.find({
  tel:7862627420
})
console.log(a.tel)
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(helmet());


app.get('/', (req,res)=>{
  res.render("index")

})

app.post('/', (req,res)=>{
  res.render("index")

})

app.post('/reglas', (req,res)=>{
  var tel = req.body.number;
  var pass = req.body.password;

})
module.exports = app;

The part that worries me is when I go to look for some data in some collection of the database. I explain. This page that I want to do requires that the user enter their phone number and a password to enter. So I was trying to find the phone number of a user in the collection that I created and for that I use a method called findOne () that passes as a parameter a json with the value I try to locate:

var a = Users.find({
      tel:7862627420
    })
    console.log(a.tel)

and then I try to show it by console that way. This returns an undefined value that I do not want, but rather that I return the number.

I do not know if I gave myself to explain well.

    
asked by Diesan Romero 22.12.2018 в 19:53
source

1 answer

0

The problem is that although mongo is sincrono Javascript and Nodejs are not so the code you have does not work for that reason you must use an anonymous function as the next parameter after the object you pass to perform the search, this function it is called callback and Nodejs and the driver that connects mongo with node works with them.
The function callback as Nodejs notation must always obtain the first parameter an error and then depending on the documentation of the function that is being used x amount of data, this means that it will be returned by means of the function callback and not the function as such.

//  mal nunca llegara a tener un valor a 
var a = Users.find({
  tel:7862627420
})
// como a no tiene nada entonces la propiedad tel no existe
console.log(a.tel)


// forma correcta
Users.find({ 
  tel:7862627420
}, function callback(error, a) {

// aquí si exite a
console.log(a)
})
    
answered by 23.12.2018 / 00:59
source