List items of an object in the JS Pug, with Node and Mongo

1

I'm setting up a store, using Keystone JS, I installed the Pug JS and MongoDB dependencies.

I created a model that is pointing to a view, called Tour.JS

The problem is that I could not efficiently list all the items that are in the MongoDB model.

var keystone = require('keystone');
var Types = keystone.Field.Types;

/**
 * User Tour
 * ==========
 */

 var Tour = new keystone.List('Tour', {
        autokey: { from: 'nome_do_passeio', path: 'key', unique: true, Singular:'Passeio', Plural: 'Passeios'},
 });

 Tour.add({
   nome_do_passeio: {type: String, label:"Nome do Passeio", initial:true},
   image: { type: Types.CloudinaryImage },
   category: { type: Types.Select, options: 'barco, vinícola', label:"Categoria", initial:true},
     custo: { type: Types.Money, format: '$0.0,00', label:"Valor", initial:true },
     data_do_passeio: { type: Types.Date, yearRange:(2000,2100), format: ('Do MMMM YYYY'), default: Date.now, label:"Data do Passeio", initial:true },
     descricao_passeio: {  type: Types.Html, wysiwyg: true, height: 200, label: "Descrição do Passeio"},
   incluido: {  type: Types.Html, wysiwyg: true, height: 200, label: "O que está incluído"},
   cidade: { type: String, required: false, index: true, label:"Cidade" }
 });


 Tour.defaultSort = Tour.data_do_passeio;
 Tour.defaultColumns = 'nome_do_passeio, custo, cidade, category, data_do_passeio';
 Tour.register();

And I would like to list all the tours that are available in my database.

But I'm using Pug JS, and I could not list it according to the amount of rides available.

extends ../layouts/default

block content

    .container
        h1 Passeio
    .container.col-md-4.col-lg-4
        .row
            if !data.tour
                h2 Passeio inválido
            else
                each tr in data.tour
                    h2= nome_do_passeio

Tour.js in "routes / views"

var keystone = require('keystone');

exports = module.exports = function(req, res) {
  var view = new keystone.View(req, res);
  var locals = res.locals;

  // Set locals
  locals.section = 'store';
  locals.filters = {
    tour: req.params.tour
  }
  locals.data = {
    tour:[]
  }

view.on('init', function(next){
  var q = keystone.list('Tour').model.findOne({
    slug: locals.filters.tour
  });

  q.exec(function(err, result){
    locals.data.tour = result;
    next(err);
  });
});

  // Render View
  view.render('tour');
}

I'm sure I'm doing something wrong, a problem that I have not been able to list all the tours using Pug JS.

I tried it in a new way.

- var qtd_nome_do_passeio = data.tour
each tr in qtd_nome_do_passeio
h2= JSON.stringify(data.tour.nome_do_passeio)

But he printed several times "Boat Ride", which is the result of the first object of the data.tour.password_name, but did not print the next object in the loop.

JSON.stringif result of data.tour

{"_id":"59f76221da96af24235f06db","key":"passeio-de-barco","nome_do_passeio":"Passeio de Barco","__v":0,"category":"barco","cidade":"Florianópolis","custo":150,"descricao_passeio":"<p><span style=\"font-family: 'Open Sans', Arial, sans-serif; text-align: justify;\">On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment</span></p>","incluido":"<p>Teste</p>","data_do_passeio":"2017-10-30T03:00:00.000Z","image":{"public_id":"o59xhtyonz6jrwts18q3","version":1509386166,"signature":"f5d2343a70a9d1ae25781d5c2ced4934a1b3e4c3","width":800,"height":528,"format":"jpg","resource_type":"image","url":"http://res.cloudinary.com/keystone-demo/image/upload/v1509386166/o59xhtyonz6jrwts18q3.jpg","secure_url":"https://res.cloudinary.com/keystone-demo/image/upload/v1509386166/o59xhtyonz6jrwts18q3.jpg"}}
    
asked by anonymous 01.11.2017 / 03:19

1 answer

0

Use .findOne( instead of .findOne( in MongoDB and then with this array can iterate like this:

each tr in data.tour
    h2= tr.nome_do_passeio
    p= tr.descricao_passeio
    p Custo: #{tr.custo}
    
01.11.2017 / 15:46