[英]ReferenceError: inCart is not defined in Node.js
I am creating a shopping cart when I started coding I am getting ReferenceError: inCart is not defined
error:当我开始编码时,我正在创建一个购物车我收到ReferenceError: inCart is not defined
错误:
ReferenceError: inCart is not defined at Object.<anonymous> (C:\Users\Win10\nodex\app.js:20:1) at Module._compile (internal/modules/cjs/loader.js:1068:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1097:10) at Module.load (internal/modules/cjs/loader.js:933:32) at Function.Module._load (internal/modules/cjs/loader.js:774:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12) at internal/main/run_main_module.js:17:47
'use strict'; const config = require('config'); class Cart { constructor() { this.data = {}; this.data.items = []; this.data.totals = 0; this.data.formattedTotals = ''; } } module.exports = new Cart(); inCart(productID = 0) { let found = false; this.data.items.forEach(item => { if (item.id === productID) { found = true; } }); return found; }
You need to define the function using the function keyword您需要使用 function 关键字定义 function
function inCart(productID = 0) {}
Or using the arrow function syntax as follows或者使用箭头 function 语法如下
const inCart = (productId=0) => {};
Please keep in mind that functions defined through function expressions must be defined before the call.请记住,通过 function 表达式定义的函数必须在调用之前定义。 Function expressions are functions that you defined through a variable keyword as follows: Function 表达式是您通过变量关键字定义的函数,如下所示:
var inCart = function(productID = 0){}
or或者
let inCart = (productID=0) => {}
You are using strict mode 'use strict'
.您正在使用严格模式'use strict'
。 When using strict mode, you cannot use undeclared variables, this will raise a Reference error.使用严格模式时,不能使用未声明的变量,这会引发引用错误。 You must declare a function using the function
keyword as shown above.您必须使用function
关键字声明 function,如上所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.