Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Like operator not works on return reply.view #397

Closed
2 tasks done
bashgaa opened this issue Oct 25, 2023 · 7 comments
Closed
2 tasks done

Like operator not works on return reply.view #397

bashgaa opened this issue Oct 25, 2023 · 7 comments

Comments

@bashgaa
Copy link

bashgaa commented Oct 25, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

4.24.3

Plugin version

No response

Node.js version

20.3

Operating system

Windows

Operating system version (i.e. 20.04, 11.3, 10)

Windows 10

Description

my login.ejs has

<% license_users.forEach((user2) => { %>
<%= user2.license_key %>
<% }) %>
  <script>
    $("#addUser_license").blur(function () {
      var myTxt = $(this).val();
      var formData = {
        "license_key": myTxt,
      };
      $.ajax({
        type: "POST",
        dataType: "JSON",
        url: "/license",
        data: formData,
      })
        .done(function (response) {
          console.log("Success: " + response.msg);
        })
        .fail(function (ts) {
          alert(ts.responseStatus);
        });
    });
  </script>

and my app.js has post method

fastify.post("/license", async (request, reply) => {
  const client = await fastify.pg.connect();
  const { license_key } = request.body;
  try {
    const result = await client.query(
      "SELECT * FROM license WHERE license_key LIKE $1",
      [license_key]
    );
    console.log(result.rows);
    console.log({ license_key });
    // Pass the search results to the EJS template
    return reply.view("/login.ejs", { license_users: result.rows });
  } catch (err) {
    console.error(err);
    return reply.view("error.ejs");
  } finally {
    client.release();
  }
});

when i write "good" on input form then in terminal window console.log(result.rows);
console.log({ license_key }); result is [ { id: 28, license_key: 'good' } ]
{ license_key: 'good' } but nothing to show in console window only "Error: undefined" occured in browser console window
i think return reply.view("/login.ejs", { license_users: result.rows }); not works. Input data not pass to login.ejs code

<% license_users.forEach((user2) => { %>
     <li><%= user2.license_key %></li>
     <% }) %>

maybe like operator not pass to template file.

Steps to Reproduce

my login.ejs has

<% license_users.forEach((user2) => { %>
<%= user2.license_key %>
<% }) %>
  <script>
    $("#addUser_license").blur(function () {
      var myTxt = $(this).val();
      var formData = {
        "license_key": myTxt,
      };
      $.ajax({
        type: "POST",
        dataType: "JSON",
        url: "/license",
        data: formData,
      })
        .done(function (response) {
          console.log("Success: " + response.msg);
        })
        .fail(function (ts) {
          alert(ts.responseStatus);
        });
    });
  </script>

and my app.js has post method

fastify.post("/license", async (request, reply) => {
  const client = await fastify.pg.connect();
  const { license_key } = request.body;
  try {
    const result = await client.query(
      "SELECT * FROM license WHERE license_key LIKE $1",
      [license_key]
    );
    console.log(result.rows);
    console.log({ license_key });
    // Pass the search results to the EJS template
    return reply.view("/login.ejs", { license_users: result.rows });
  } catch (err) {
    console.error(err);
    return reply.view("error.ejs");
  } finally {
    client.release();
  }
});

when i write "good" on input form then in terminal window console.log(result.rows);
console.log({ license_key }); result is [ { id: 28, license_key: 'good' } ]
{ license_key: 'good' } but nothing to show in console window only "Error: undefined" occured in browser console window
i think return reply.view("/login.ejs", { license_users: result.rows }); not works. Input data not pass to login.ejs code

<% license_users.forEach((user2) => { %>
     <li><%= user2.license_key %></li>
     <% }) %>

maybe like operator not pass to template file.

Expected Behavior

No response

@Uzlopak
Copy link
Contributor

Uzlopak commented Oct 25, 2023

Closing this because I moved the other issue in fastify core to fastify/help.

Also an issue in fastify/core was already closed by @jsumners

@Uzlopak Uzlopak closed this as completed Oct 25, 2023
@bashgaa
Copy link
Author

bashgaa commented Oct 25, 2023

hello sorry again

i uploaded to github and respository is

https://github.com/bashgaa/example_like_operator

@bashgaa
Copy link
Author

bashgaa commented Oct 25, 2023

pls check this my project section

@bashgaa
Copy link
Author

bashgaa commented Oct 25, 2023 via email

@bashgaa
Copy link
Author

bashgaa commented Oct 27, 2023

Anyone authors see my project?

@Eomm
Copy link
Member

Eomm commented Oct 30, 2023

Don't be pushy
This issue duplicates: fastify/help#952

Whenever someone will have time, will take a look.
More issue you open, less answers you will get.

Right now I would suggest you to divide your issue in smaller problems:

  1. remove postgres
  2. return static fake data: return reply.view("/login.ejs", { license_users: [STATIC DATA] });
  3. does it work?
  4. if it works the issue is on postgres
  5. otherwise remove another piece of code till you find the issue
  6. etc...

@bashgaa
Copy link
Author

bashgaa commented Oct 30, 2023

return static fake data: return reply.view("/login.ejs", { license_users: [STATIC DATA] });
still get undefined error

@fastify fastify locked as off-topic and limited conversation to collaborators Nov 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants