• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    해결됨

상품추천api오류

24.01.18 16:10 작성 24.01.18 16:43 수정 조회수 129

1

//상품 추천 api (feat: tensoflow)
app.get("/products/:id/recommendation", (req, res) => {
  const { id } = req.params;

  //findOne으로 req을 통해 받아온 param값 id에 맞는 상품을조회한다.
  models.Product.findOne({
    where: {
      id,
    },
  })
    .then((product) => {
      //id와 일치하는 상품에서 type값을 뽑아서,
      const type = product.type;
      //type값과 일치하는 상품들을 모두찾는다.
      models.Product.findAll({
        where: {
          type,
          id: {
            //기준이되는 id와 일치하지않는 데이터만찾겠다.
            //예를들어 id가4번일때 4번을제외한 4번과 같은type의 상품만 보여줘야하는데
            //4번도 함께 추천이되니, 4번을 제외하게해준다.
            [models.Sequelize.Op.ne]: id,
          },
        },
      }).then((products) => {
        res.send({
          products,
        });
      });
    })
    .catch((error) => {
      console.error(error);
      res.status(500).send("에러가 발생했습니다..");
    });
});

server.js에서 추천api를 작성하고

 

웹에서 확인하려고하면

 

에러내용

TypeError: Cannot read properties of null (reading 'type')

at /Users/kimsehun/Desktop/market-prj/h-market-server/server.js:191:28

 

이런 에러가발생합니다.

models-product.js

module.exports = function (sequelize, DataTypes) {
  const product = sequelize.define("Product", {
    name: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    price: {
      type: DataTypes.INTEGER(10),
      allowNull: false,
    },
    seller: {
      type: DataTypes.STRING(30),
      allowNull: false,
    },
    description: {
      type: DataTypes.STRING(300),
      allowNull: false,
    },
    imageUrl: {
      type: DataTypes.STRING(300),
      allowNull: true,
    },
    soldout: {
      type: DataTypes.INTEGER(1),
      allowNull: false,
      defaultValue: 0,
    },
    type: {
      type: DataTypes.STRING(50),
      allowNull: true,
    },
  });
  return product;
};

 계속보고있는데,
findOne부분에서 where을 통해 id값에 해당하는 상품을
못찾아서 product에 데이터가 담기지않아서,
type을 못불러오는거같은데.

뭐가문제일까요??

 

답변 1

답변을 작성해보세요.

1

해결했습니다..!!