将其值传递给函数后,对象突然变得不确定

时间:2019-05-27 18:30:13

标签: javascript

Object becomes undefined after passing to a function as a parameter.

当我在将它返回并将其传递给另一个函数之前,将其像下面那样进行console.log记录时,它正在正确输出:

const fetchCart = Cart.fetchAll((cart) => {
  console.log(cart); //outputs the object without any problem.
  return cart;
})

但是当我将其作为参数传递给以下函数时,它变得不确定,可以通过键入console.log并从错误消息中看到它:

const checkIfThereIsAMatch = (products , cart)=>{
  console.log(cart); //outputs undefined.
  if(products.find(o=>{cart.products.id === products.id})){
    return true;
  } else {
    return false;
  }
}

//我的代码在这里:

static checkIfCartItemAvaible(){


  function check (products , cart ,checkIf){
    if(checkIf(products , cart) === true){return true;}else{return false;}
  }

  const fetchProducts = Product.fetchAll((products) => {
    return products;
  }) 


  const fetchCart = Cart.fetchAll((cart) => {
    return cart;
  }) 

  const checkIfThereIsAMatch = (products , cart)=>{
    if(products.find(o=>{cart.products.id === products.id})){
      return true;
     } else {
      return false;
    }
  }

  return check(fetchProducts , fetchCart , checkIfThereIsAMatch);
} 

获取以下错误消息:

Users/user/Desktop/my website project/models/cart.js:272
        if(products.find(o=>{cart.products.id === products.id})){
                    ^

TypeError: Cannot read property 'find' of undefined
at checkIfThereIsAMatch (/Users/user/Desktop/my website project/models/cart.js:272:25)
at check (/Users/user/Desktop/my website project/models/cart.js:238:24)
at Function.checkIfCartItemAvaible (/Users/user/Desktop/my website project/models/cart.js:280:15)
at /Users/user/Desktop/my website project/controllers/shop.js:99:32
at /Users/user/Desktop/my website project/models/cart.js:43:13
at FSReqCallback.readFileAfterClose [as oncomplete] (internal/fs/read_file_context.js:61:3)
[nodemon] app crashed - waiting for file changes before starting...

0 个答案:

没有答案
相关问题