0

I need to loop through an object PostIts and display the "Id", " Title" with an ejs "forEach" Loop Am using sails.js "1.2.3" and mongodb on local host, but i get error

ReferenceError : postIts is not defined at eval (eval at compile ?

Here is the code on the PostItsController.js:

module.exports = {

    list: function(req, res) {
        // res.view('list');
        PostIts.find({}).exec(function(err, postIts) {
            if (err) {
                res.send(500, { error: 'Database Error' });
            }
            res.view('list', { postIts: postIts });
        });
    }
};

And here is the code on list.ejs:

     <tbody>
       <% postIts.forEach(function(postit){ %>
            <tr>
                <td>
                    <%= postit.id %>
                </td>
                <td>
                    <%= postit.title %>
                </td>
                <td></td>
            </tr>
            <% }) %>
       </tbody>

I should get the value of the ID and title displayed on the list.ejs page in a table, but instead I get an error that the postIts object is not defined.

ThanhPhanLe
  • 1,315
  • 3
  • 14
  • 25
zak
  • 3
  • 4
  • UPDATE: I found this on a Github " Sails Changelog" saying : Also in new Sails apps: Use for instead of _.each(). I tried searching for for loops with ejs but still end up with an error. – zak Jul 26 '19 at 05:48
  • here is the new code for the loop: <% for( var i=0; i< postIts.length; 1++){ %> <%= postIts[i].id %> <%= postIt[i].title %> <% } %> – zak Jul 26 '19 at 05:50
  • and this is the new error message: ReferenceError: Invalid left-hand side expression in postfix operation at new Function () at Template.compile . – zak Jul 26 '19 at 05:51

3 Answers3

1

First of all your route '/postIts/list': { view: 'list' }, should point to an action (since it has backend logic) not a view, so in your case "/postIts/list": "PostItsController.list", but if you're using actions2 things would be simpler

Secondly you don't need to tell your users that you have a database error error: "Database Error"

Using Actions2

sails generate action post/list

In your config/route.js

 'POST /api/v1/post/list': { action: 'post/list' },

In your action

module.exports = {
  friendlyName: "List Posts",

  description: "List all post in our site",

  inputs: {},

  exits: {
    success: {
      description: "The path to your template file",
      viewTemplatePath: "list"
    }
  },

  fn: async function(inputs) {
    var posts = await Post.find();
    // All done.
    return { postIts: posts };
  }
};

postit works

An Boohoo! it works https://sailsjs.com/documentation/concepts/actions-and-controllers/routing-to-actions

  • You are awesome man ! how did u make it work ? haha. This one sounds more interesting :) ! so if I will be following the Actions2 , I will not change anything in the list.ejs and just change the route and plus adding the new action ? – zak Jul 30 '19 at 02:20
  • Too late now I guess, but you should usually edit your previous answer to update it, rather than post a new independent answer. – tripleee Jul 30 '19 at 08:17
0

If you're sure that res.view('list', { postIts: postIts }); is actually sending the correct data you can use _.each(postIts, cb()) ... instead

  • thanks for the reply. Am not really sure cuz am so new to API and sails as well, I was following this tutorial on youtube and I got stuck here, I also saw other comments same with mine on that video but not response. [https://www.youtube.com/watch?v=JoAozHe7ejg] – zak Jul 29 '19 at 05:26
  • Here is a link to this sails app on github [https://github.com/hkzak/Sails-App.git]. By the wayHow can I test if 'res.view('list', { postIts: postIts });' is sending the correct data? – zak Jul 29 '19 at 05:29
0

For some reason the postIts object didnt save the data from the post req I made instead it just recalled what I posted. and I used the '_.each(postIts, function (postit)' and it finally worked.

to me its like a magic happened hahaha but yeah I learned from it.

thanks @Navicstein Rotciv for the quick replies.

zak
  • 3
  • 4