Sails: Res.ok() returning data as string and not as object to EJS views

Created on 8 Sep 2016  路  10Comments  路  Source: balderdashy/sails

Sails version: v0.12.3
Node version: v.4.4.3
NPM version: 3.10.2
Operating system: OS.X 10.11.5


I've been following the documentation for the res.ok() response method and have implemented it exactly as documented.

However, when targeting the JSON object values of the data injected into the EJS template, I get undefined.

_Controller_

return res.ok({
  name: 'Loic',
  occupation: 'developer'
}, 'testpage');

_testpage.ejs - using layout.ejs_

<body>
      <h1>Test Page</h1>
      <p><%= data %></p>
      <p><%= data.name %></p>
      <p><%= data.occupation %></p>
      <p><%= typeof(data) %></p>
</body>

_This renders as (via page source) from Chrome:_

<body>
    <h1>Test Page</h1>
      <p>{ name: &#39;Loic&#39;, occupation: &#39;developer&#39; }</p>
      <p>undefined</p>
      <p>undefined</p>
      <p>string</p>
</body>

I've noticed that the data is injected as a string and I presume that it should be an object. When using res.view(), using typeof(data) returns as object.

inconsistency try this out please

Most helpful comment

I've had this problem in the past and seeing it again. To fix I changed /api/responses/ok.js.

From: if (!(viewData instanceof Error) && 'object' == typeof viewData) {
To: if ((viewData instanceof Error) && 'object' == typeof viewData) {

Get rid of the '!'.

Make sure to restart sails to take effect...

All 10 comments

@cbarlow1993: Hello, I'm a repo bot-- nice to meet you!

It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message and simply close the issue if possible. On the other hand, if you are still waiting on a patch, please post a comment to keep the thread alive (with any new information you can provide).

If no further activity occurs on this thread within the next 3 days, the issue will automatically be closed.

Thanks so much for your help!

Still haven't seen a fix / comment on this one.

@cbarlow1993 Thanks for the detailed explanation!

I tested this on Sails v0.12.7 with:

image

image

And got:

image


@cbarlow1993 Would you try running npm install to get the latest 0.12.x release and verify that it works as expected for you as well?

Hi @mikermcneil ,

I've just tried using sails v0.12.7 and still getting the issue as described in my previous report. Strange that you are seeing different.

I just tested using a newly generated project.

I created a route with sails generate controller info:

module.exports = {
    info: function(req, res) {
        return res.ok({
            name: 'Loic',
            occupation: 'developer'
        }, 'homepage')
    }
};

edited config/routes.js to include a line '/info': 'infoController.info':

edited homepage.ejs. Removed all in <div class="default-page"> and replaced with

<div class="default-page">
      <h1>Test Page</h1>

      <h3>data</h3>
      <p><%= data %></p>

      <h3>typeof data</h3>
      <p><%= typeof(data) %></p>

      <h3>data.name</h3>
      <p><%= data.name %></p>

      <h3>data.occupation</h3>
      <p><%= data.occupation %></p>
</div>

Run app with sails lift. Sails art shows v0.12.7 running. Same as running sails --version

node --version is v4.4.5`

npm list ejs = @2.3.4

npm list -g --depth=0 = @2.5.2
Not so pretty image below of results but may be useful to you:

image

Upgraded node version to v6.9.1. Still issue remains.

Hi @mikermcneil, any further thought's on this one? Thanks.

@cbarlow1993 would you mind pasting the contents of api/responses/ok.js and .sailsrc? Maybe something weird going on w/ a generator or custom response? Having a hard time figure this out, since I can't replicate :/

Is anyone else able to replicate this locally? Maybe there's something system-specific on my end

@mikermcneil Did you find a fix for this? I am trying to do the same send data to the view via a controller action.

<% _.each(forms, function(form){ %>
<tr data-expanded="false" id="form-records" data-id="<%= form.formid %>" data-model="forms">
   <td><%= form.formname %></td>
   <td><%= form.formid %></td>
   <td>
      <form action="/forms/getSecGroup?formid=<%= form.formid %>" method="POST">
         <% data.records %>
      </form>
   </td>
</tr>

is in my forms.ejs file and my controller action is

'getSecGroup': function(req, res, next){
   Forms.find().where({formid: req.param('formid')}).populateAll().exec(function (err, records) {
            console.log(records[0].securitygroup[0].secname);
            var records = records[0].securitygroup[0].secname;
            return res.ok({records: records}, 'forms/index');
        });
    }

it console logs the correct values but it is not being passed into my ejs view. I get a sails.js error "data is not defined"

Hi cbarlow1993, I麓have this same problem, i need your help please, what's the solution for this problem

I've had this problem in the past and seeing it again. To fix I changed /api/responses/ok.js.

From: if (!(viewData instanceof Error) && 'object' == typeof viewData) {
To: if ((viewData instanceof Error) && 'object' == typeof viewData) {

Get rid of the '!'.

Make sure to restart sails to take effect...

Removing the '!'. Solved! Thx @ddevalk

Was this page helpful?
0 / 5 - 0 ratings

Related issues

edy picture edy  路  4Comments

svmn picture svmn  路  4Comments

thomasfr picture thomasfr  路  3Comments

Noitidart picture Noitidart  路  4Comments

3imed-jaberi picture 3imed-jaberi  路  3Comments