简体   繁体   English

找不到模块“js”

[英]Cannot find module 'js'

After adding a request I've got such error.添加请求后,我遇到了这样的错误。 I tried npm install js and adding var js = require("js") in my app.js file, but it didn't help at all.我试过npm install js并在我的 app.js 文件中添加var js = require("js") ,但它根本没有帮助。 I run an express server on my localhost.我在我的本地主机上运行一个快速服务器。

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)

This is the request I've added这是我添加的请求

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 didn't help. npm 安装 js 没有帮助。 Here is my package.json这是我的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"
  }
}

It seems that the problem in those js functions filter and find , but I have no idea how to make them understandable for Node.似乎这些 js 函数中的问题filterfind ,但我不知道如何让它们对 Node.js 来说是可以理解的。

UPDATE: It is how items look like.更新:这是物品的样子。 I need to filter them by category.我需要按类别过滤它们。 I want to show only those ones, who have filtering value in field category .我只想显示那些在字段category中具有过滤值的那些。

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]

    }
] 

I've got a typo here.我这里有一个错字。 index.js instead of index.ejs in res.render() . index.js而不是res.render ()中的 index.ejs。 This one is correct这个是对的

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});
});
``

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM