I am creating a shopping cart when I started coding I am getting ReferenceError: inCart is not defined
error:
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 inCart(productID = 0) {}
Or using the arrow function syntax as follows
const inCart = (productId=0) => {};
Please keep in mind that functions defined through function expressions must be defined before the call. Function expressions are functions that you defined through a variable keyword as follows:
var inCart = function(productID = 0){}
or
let inCart = (productID=0) => {}
You are using strict mode '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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.