用nodejs搜索JSON
我一直在尝试实施搜索栏。我的想法是用户可以部分键入单词,然后我可以在响应中传递变量,以便在我的EJS中使用。我不是故意使用SQL数据库,因为IM试图以JSON文件作为数据库保持此站点的运行。我已经尝试过,但似乎不能使它起作用。我尝试了多种方法。
让Usersearch =(req.body.search) 让AllProducts = Data
let filteredProducts = allProducts.filter(x => x.title == x.title.indexOf(userSearch))
console.log(allProducts), gives you this :
[
{
id: 1,
title: 'Chicken and vegetables stir-fry',
Ingredients: [
'2 tbsp peanut oil',
'3 Lilydale Free Range Chicken Breast, trimmed, cut across the grain into thin strips',
'1 brown onion, cut into thin wedges',
'1 red capsicum, deseeded, cut into thin strips',
'250g button mushrooms or mushroom caps, sliced',
'1 bunch broccolini, cut into 4cm lengths',
'2cm piece fresh ginger, peeled, finely grated or chopped',
'2 garlic cloves, finely chopped',
'2 fresh birdseye chillies, deseeded if desired, thinly sliced',
'2 tbsp salt-reduced soy sauce',
'1 1/2 tbsp oyster sauce',
'1 tbsp water',
'100g bean sprouts, to serve',
'Coriander leaves, to serve',
'Cooked white rice or noodles, to serve'
],
description: 'Delicious and nutricious',
image: 'chickenAndVeggies.jpeg',
belongsTo: '[email protected]'
},
{
id: 2,
title: 'Chocolate fudge cake',
Ingredients: [
'2 150ml sunflower oil, plus extra for the tin',
'175g self-raising flour',
'2 tbsp cocoa powder',
'1 tsp bicarbonate of soda',
'150g caster sugar',
'2 tbsp golden syrup',
'2 large eggs, lightly beaten',
'150ml semi-skimmed milk'
],
description: 'What´s better than a choco cake?',
image: 'chococake.jpeg',
belongsTo: '[email protected]'
},
{
id: 3,
title: 'Quiche Lorraine',
Ingredients: [
'1/8 tsp ground nutmeg',
'1/4 tsp smoked paprika',
'1/4 tsp black pepper, finely ground',
'1/2 tsp kosher salt',
'4 eggs, large',
'1 3/4 cup half and half, (or equal parts whole milk and heavy cream)',
'1 cup shredded cheese , (I used sharp white cheddar, but gruyere is classic)',
'1/2 yellow onion, diced',
'1/2 lb thin strips of bacon, chopped into 1/2 inch pieces',
'1 pie crust, (1 frozen crust, or 1/2 recipe for double pie crust)'
],
description: 'A delicious quiche',
image: 'QuicheLorraine.jpeg',
belongsTo: '[email protected]'
},
{
id: 4,
title: 'Fluffy pancakes',
Ingredients: [
'1 cup all-purpose flour',
'2 tablespoons white sugar',
'2 teaspoons baking powder',
'1 teaspoon salt',
'1 egg, beaten',
'1 cup milk',
'2 tablespoons vegetable oil'
],
description: 'Easy to make, delicious to eat',
image: 'fluffypancakes.jpeg',
belongsTo: '[email protected]'
},
{
id: 5,
title: 'Carrot cake',
Ingredients: [
'2 cups (240g) all-purpose flour',
'2 teaspoons baking powder',
'2 teaspoons cinnamon',
'2 teaspoons allspice ',
'1/2 teaspoon salt',
'4 large eggs',
'2 cups (400g) granulated sugar',
'1 1/4 cup vegetable oil',
'3 cups loosely packed peeled and grated carrots'
],
description: 'mmmm a dream!',
image: 'carrot-cake.jpeg',
belongsTo: '[email protected]'
},
{
id: 6,
title: 'Kitty kitty',
description: 'Beautiful',
Ingredients: [ 'kitty', ' nothing', ' test', ' test', ' test' ],
image: 'image-1654609682017.jpeg',
belongsTo: '[email protected]'
}
]
Console.log(userSearch)为您在搜索栏输入中键入的输入,该输入来自体内。在这种情况下,我输入了Hello So:
您好,
console.log(filteredProducts) gives an empty array regardless
[]
例如,即使复制标题AvateClty,也会返回一个空数组。 在这种情况下如何实施搜索栏有什么好主意?
ps我不知道jQuery,但我使用nodejs的简单JS。谢谢!
i have been trying to implement a search bar. My idea is that the user can partially type the word and then i can pass the variable in the response so it is used in my EJS. I am not using SQL databases on purpose, as im trying to keep this site running only with JSON file as database. I have tried but cant seem to make it work. I have tried many ways.
let userSearch = (req.body.search)
let allProducts = data
let filteredProducts = allProducts.filter(x => x.title == x.title.indexOf(userSearch))
console.log(allProducts), gives you this :
[
{
id: 1,
title: 'Chicken and vegetables stir-fry',
Ingredients: [
'2 tbsp peanut oil',
'3 Lilydale Free Range Chicken Breast, trimmed, cut across the grain into thin strips',
'1 brown onion, cut into thin wedges',
'1 red capsicum, deseeded, cut into thin strips',
'250g button mushrooms or mushroom caps, sliced',
'1 bunch broccolini, cut into 4cm lengths',
'2cm piece fresh ginger, peeled, finely grated or chopped',
'2 garlic cloves, finely chopped',
'2 fresh birdseye chillies, deseeded if desired, thinly sliced',
'2 tbsp salt-reduced soy sauce',
'1 1/2 tbsp oyster sauce',
'1 tbsp water',
'100g bean sprouts, to serve',
'Coriander leaves, to serve',
'Cooked white rice or noodles, to serve'
],
description: 'Delicious and nutricious',
image: 'chickenAndVeggies.jpeg',
belongsTo: '[email protected]'
},
{
id: 2,
title: 'Chocolate fudge cake',
Ingredients: [
'2 150ml sunflower oil, plus extra for the tin',
'175g self-raising flour',
'2 tbsp cocoa powder',
'1 tsp bicarbonate of soda',
'150g caster sugar',
'2 tbsp golden syrup',
'2 large eggs, lightly beaten',
'150ml semi-skimmed milk'
],
description: 'What´s better than a choco cake?',
image: 'chococake.jpeg',
belongsTo: '[email protected]'
},
{
id: 3,
title: 'Quiche Lorraine',
Ingredients: [
'1/8 tsp ground nutmeg',
'1/4 tsp smoked paprika',
'1/4 tsp black pepper, finely ground',
'1/2 tsp kosher salt',
'4 eggs, large',
'1 3/4 cup half and half, (or equal parts whole milk and heavy cream)',
'1 cup shredded cheese , (I used sharp white cheddar, but gruyere is classic)',
'1/2 yellow onion, diced',
'1/2 lb thin strips of bacon, chopped into 1/2 inch pieces',
'1 pie crust, (1 frozen crust, or 1/2 recipe for double pie crust)'
],
description: 'A delicious quiche',
image: 'QuicheLorraine.jpeg',
belongsTo: '[email protected]'
},
{
id: 4,
title: 'Fluffy pancakes',
Ingredients: [
'1 cup all-purpose flour',
'2 tablespoons white sugar',
'2 teaspoons baking powder',
'1 teaspoon salt',
'1 egg, beaten',
'1 cup milk',
'2 tablespoons vegetable oil'
],
description: 'Easy to make, delicious to eat',
image: 'fluffypancakes.jpeg',
belongsTo: '[email protected]'
},
{
id: 5,
title: 'Carrot cake',
Ingredients: [
'2 cups (240g) all-purpose flour',
'2 teaspoons baking powder',
'2 teaspoons cinnamon',
'2 teaspoons allspice ',
'1/2 teaspoon salt',
'4 large eggs',
'2 cups (400g) granulated sugar',
'1 1/4 cup vegetable oil',
'3 cups loosely packed peeled and grated carrots'
],
description: 'mmmm a dream!',
image: 'carrot-cake.jpeg',
belongsTo: '[email protected]'
},
{
id: 6,
title: 'Kitty kitty',
description: 'Beautiful',
Ingredients: [ 'kitty', ' nothing', ' test', ' test', ' test' ],
image: 'image-1654609682017.jpeg',
belongsTo: '[email protected]'
}
]
console.log(userSearch) gives you the input you typed in the search bar input, the one coming in the body. In this case I typed hello so:
hello
console.log(filteredProducts) gives an empty array regardless
[]
This for example, returns an empty array even when copying the title exaclty.
Any good ideas on how to implement the search bar in this case?
P.S i don´t know Jquery yet i use simple JS with NodeJs. thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论