1
I’m setting up a store, using Keystone JS, installed the dependencies Pug JS and Mongodb.
I created a model that is pointing to a view, called Tour.JS
The problem is that I couldn’t 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 am using Pug JS, and could not list according to the amount of tours 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');
}
Surely I am doing something wrong, problem that I could not so far list all the tours using the Pug JS.
I tried 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 date.tour.path_name, but did not print the next in the loop.
JSON.stringif result of the date.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"}}
Ok, now JSON is already in the question. This json is only of one object, there is nothing to iterate there... this is the result of
JSON.stringify(data.tour)
? So on Node.js how do you pass thedata
for Pug? can show how you create this variabledata
?– Sergio
I put in question the file Tour.js in "Routes/views".
– Pedro Magalhaes
Okay, what if you put it the way it was
.findOne
use the.find()
?– Sergio
Great!!! IT WORKED! THANK YOU VERY MUCH!!!!
– Pedro Magalhaes