I cannot return the requested amount ( Mongodb )

Asked

Viewed 35 times

0

I’m trying to make a list of names of people who are registered in my database, and print on the web

The model I’m using is this

const mongoose = require('../../database/conn')
const bcrypt = require('bcryptjs')

function adicionaZero(numero){
    if (numero <= 9) 
        return "0" + numero
    else
        return numero
}

var dataAtual = new Date()
var dataAtualFormatada = (adicionaZero(dataAtual.getDate().toString()) + "/" + (adicionaZero(dataAtual.getMonth()+1).toString()) + "/" + dataAtual.getFullYear())

const UserSchema = new mongoose.Schema({
    nome: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true,
        lowercase: true
    },
    senha: {
        type: String,
        required: true,
        select: false
    },
    senhaResetToken: {
        type: String,
        select: false
    },
    senhaResetExpires: {
        type: Date,
        select: false
    },
    data_registro: {
        type: String,
        required: true,
        default: dataAtualFormatada
    }
})

UserSchema.pre('save', async function(next) {
    const hash = await bcrypt.hash(this.senha, 10)
    this.senha = hash

    next()
})

const User = mongoose.model('User', UserSchema)

module.exports = User

Router

router.get('/comunidade', async (req, res) => {
const cookieCheck = req.cookies['PdSO']
const perfil = await User.findOne({ "_id": cookieCheck['id'] })

User.find().then((membros) => {
            if(!cookieCheck)
                return res.send("VC N ESTA LOGADO")

                return res.render("pages/index/comunidade", {
                "membros": membros,
                "nomes": nome,
                "css_file": "/comunidade.css"
                
        }).catch ( (err) => {
            console.log("ERR: " + err)
        })
    })
})

Handlebars

                <div class="container-comunidade">
                    <h4>Comunidade</h4>
                    <div class="container-comunidade-wraper">
                        {{> membros }}
                    </div><!--/container-comunidade-wraper-->
                </div><!--/container-comunidade-->

Handlebars.

{{#each membros}}

                        <div class="container-comunidade-single">
                            <div class="img-comunidade-user-single">
                                <img src="/img/test/avatar.jpg" />
                            </div><!--/img-comunidade-user-single-->
                            <div class="info-comunidade-user-single">
                                <h2> {{ nomes }} </h2>
                                <br />
                            <div class="btn-solicitar-amizade">
                                <a href="#">Solicitar Amizade</a>
                            </div><!--btn-solicitar-amizade-->
                            </div><!--info-comunidade-user-single-->
                        </div><!--/container-comunidad-single-->
{{else}}

                        <div class="container-comunidade-single">
                            <div class="img-comunidade-user-single">
                                <img src="/img/test/avatar.jpg" />
                            </div><!--/img-comunidade-user-single-->
                            <div class="info-comunidade-user-single">
                                <h2> 0 </h2>
                                <br />
                            <div class="btn-solicitar-amizade">
                                <a href="#">Solicitar Amizade</a>
                            </div><!--btn-solicitar-amizade-->
                            </div><!--info-comunidade-user-single-->
                        </div><!--/container-comunidad-single-->
{{/each}}

PS: Shows profiles of registered members, but does not display names of any of them

I thank you already ah everyone, and sorry for the size of the quest is that I am an animal and I’m learning yet.

  • Hey hi! You’ve tried db.collection.find({}, { nome: 1, _id: 0 }); ?

2 answers

0

Oops? I’ll give a nice twist on your code, just to make it a little more organized, and Voce tries there, we will change the router (I think the way that Voce is calling the obj is incorrect).

     async function index(membros) {
     try {
      if(!cookieCheck) {
       return "voce nao esta logado";
       }
           this.user = async User.find();
           const nomeUsers = this.user.data[1].nome;

         return res.render("pages/index/comunidade", {
                "nomes": this.nomeUsers,
                "css_file": "/comunidade.css"

    } 
} 
   catch(e) {
    
     console.log(e);
       
     }
   }

hope it works

  • 1

    forgot to put this in const nameUsers, change it to this.nameUsers = this.user.data[1]. name;

  • You can edit the reply to add the missing information

  • Oops, I tested here did not work, page did not load or appeared any error on the console

0


Opa I came back here, I found a way to leave the listing the way I want, I just want to add the "query" Lean after find and do not need to put the name value on the route it already appears automatically.

router.get('/comunidade', async (req, res) => {
const cookieCheck = req.cookies['PdSO']
const perfil = await User.findOne({ "_id": cookieCheck['id'] })

User.find().lean().then((membros) => {
            if(!cookieCheck)
                return res.send("VC N ESTA LOGADO")

                return res.render("pages/index/comunidade", {
                "membros": membros,
                "css_file": "/comunidade.css"
             })
                
        }).catch ( (err) => {
            console.log("ERR: " + err)
        })
})

Thank you all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.