添加请求后,出现了此类错误。我尝试了 npm install js ,并在我的app.js文件中添加了 var js = require(“ js”),但这根本没有帮助。我在本地主机上运行快递服务器。
Error: Cannot find module 'js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:611:15)
at Function.Module._load (internal/modules/cjs/loader.js:537:25)
at Module.require (internal/modules/cjs/loader.js:665:17)
at require (internal/modules/cjs/helpers.js:20:18)
at new View (E:\me\univercity\ais_node\cashdesk\node_modules\express\lib\view.js:81:14)
at Function.render (E:\me\univercity\ais_node\cashdesk\node_modules\express\lib\application.js:570:12)
at ServerResponse.render (E:\me\univercity\ais_node\cashdesk\node_modules\express\lib\response.js:1012:7)
at E:\me\univercity\ais_node\cashdesk\app.js:201:9
at Layer.handle [as handle_request] (E:\me\univercity\ais_node\cashdesk\node_modules\express\lib\router\layer.js:95:5)
at next (E:\me\univercity\ais_node\cashdesk\node_modules\express\lib\router\route.js:137:13)
这是我添加的请求
app.get("/category/:id", function(req, res){
var id = parseInt(req.params.id.charAt(1));
itemsCopy = items.filter(function(el){
console.log(el);
return el.category.find(c => c == id);
});
console.log(itemsCopy);
res.render("index.js", {items: itemsCopy});
});
npm install js没有帮助。 这是我的 package.json
{
"name": "blog",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"body-parser": "^1.19.0",
"ejs": "^3.1.3",
"express": "^4.17.1",
"js": "^0.1.0"
}
}
似乎这些js函数 filter 和 find 中的问题,但是我不知道如何使它们对于Node来说是可理解的。
更新: 这就是物品的外观。我需要按类别过滤它们。我只想显示在字段类别中具有过滤值的那些。
items = [
{
name: "Lorem",
price: 16,
descr: "This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.",
warranity: true,
warranity_length: 6,
warranity_rules: "Do not put under water",
category: [0]
},
{
name: "ipsum",
price: 67,
descr: "This is a wider card with supporting text below as a natural lead-in to additional content. This content is a little bit longer.",
warranity: true,
warranity_length: 6,
category: [0,1]
}
]
答案 0 :(得分:0)
我这里有错字。 index.js 代替 res.render()中的 index.ejs 。这是正确的
app.get("/category/:id", function(req, res){
var id = parseInt(req.params.id.charAt(1));
itemsCopy = items.filter(function(el){
console.log(el);
return el.category.find(c => c == id);
});
console.log(itemsCopy);
res.render("index.ejs", {items: itemsCopy});
});
``