Home>
Problem i am experiencing
The following data is stored in the database.
id name comment
1 curry test1
2 stew test2
3 curry test3
I search for the name of the dish (name) and display the result, but if there is only one like "stew", the search result can be displayed, but "curry" If there are two or more like, only one search result can be displayed.
In order to display all the search results, I changed fetch to fetchAll and tried to loop the part that displays the search results with for, but it did not work.
How can I display all the search results?
If the search result is 0 (for example, search for "miso soup"), the search will end and the page will not be updated.
I hope you can teach me how to solve it if possible.
Thank you.
<body>
<form method = "post" action = "/ hello/add" name = "test">
<span>Dish name:</span>
<input type = "text" name = "name" value = "<% = form.name%>">
</td><input type = "submit" value = "search">
</form>
<% if (mydata! = null) {%>
<p><ul>
<li><%-mydata.id%></li>
<li><%-mydata.attributes.name%></li>
<li><%-mydata.attributes.comment%></li>
</ul></p>
<%}%>
</body>
var express = require ('express');
var router = express.Router ();
var mysql = require ("mysql");
var knex = require ("knex") ({
dialect: "mysql",
connection: {
host: "localhost",
user: "root",
password: "",
database: "db",
charset: "utf8"
}
});
var Bookshelf = require ("bookshelf") (knex);
var MyData = Bookshelf.Model.extend ({
tableName: "mydata"
});
// MySQL configuration information
var mysql_setting = {
host: "localhost",
user: "root",
password: "",
database: "db"
};
// ADD
router.get ("/ add", (req, res, next) =>{
var data = {
form: {name: ""},
mydata: null,
}
res.render ("hello/add", data);
});
router.post ('/ add', (req, res, next) =>{
new MyData (). where ("name", "=", req.body.name) .fetch (). then ((collection) =>{
var data = {
form: req.body,
mydata: collection,
};
res.render ("hello/add", data);
})
});
module.exports = router;
-
Answer # 1
Related articles
- java - i want to display search results
- javascript - i want to display a local image in nodejs
- [javascript] i want to display an alert so that i can not search if the form made with html is blank
- ruby on rails - i want to display search results
- typescript - i want to display search results by hitting an external api with react
- i want to return typescript or javascript method results in vue
- javascript - i want to display leaflet pins in a cluster
- javascript - individual retrieval and display from associative arrays
- javascript - how to delete the item of json array read by nodejs?
- javascript - i want to display vertical navigation on both sides now
- i want to display characters in javascript p tag
- javascript - i want to shift the display of the array
- javascript - i want to display pdf from url using react-pdf
- php - to display the record of the search condition specified in where
- javascript - i want to change the display by selecting the radio button
- wordpress - keeping checkboxes in search results
- i want to make a 〇 × game (tic tac toe) with javascript i want to display 〇 × on the board
- javascript - i want to be able to search for posts on the same browser by clicking the image
- javascript - i want to set the truth using the library called express and joi of nodejs and check if it works normally with post
- javascript - i want to display a drop-down menu using css and js in html
Related questions
- javascript : How to fix this bug, when saving data to a file, an empty line is added at the end of the NodeJS module fs
- javascript : Error: The N-API version of this Node instance is 1. This module supports N-API version (s) 3. This Node instance c
- javascript : Find a digit in a string and print the string after the number [JS]
- javascript : VK authorization using passport.js
- javascript : Regular expression triggered on the first character
- javascript : How to increase data limit in new Map ()
- javascript : How to get transferred data from client to server?
- mysql : cookie not working, connected to node js
- javascript : POST request to add a record to an array (sequelize)
- javascript : I created a Discord bot in JS, but it gives me an error
If you execute a query and return multiple results, it is a two-layer object, so you can write it in the same way.
Since it is, the process to loop the object is necessary in the template or script.