0

I can't seem to find a problem why this is not working. When I go to localhost:3000 I get this error ReferenceError: pixwords is not defined

app.get('/', (req, res) => {
Word.find().then((pixwords) => {
    if(!pixwords) {
     return res.send('No text available');
    }
});
res.render('index.hbs', {pixwords});
});

This is index.hbs:

{{#each pixwords}}
    {{this.text}}
{{/each}}
George
  • 6,630
  • 2
  • 29
  • 36

1 Answers1

2

pixwords is only defined in the callback function of Word.find().then(), you can only use it there:

app.get('/', (req, res) => {
  Word.find().then((pixwords) => { // <-- this declares pixwords
    if (!pixwords) {
      return res.send('No text available');
    }
    res.render('index.hbs', { // <-- move the call in the .then
      pixwords
    });
  });
  // anything here is executed before Word.find has finished running
});
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44