• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

삭제 하니까 Product.find(...).populdate is not a function 라고 뜹니다 ㅠ

21.01.29 19:07 작성 조회수 84

0

] events.js:292

[0]       throw er; // Unhandled 'error' event

[0]       ^

[0]

[0] TypeError: Product.find(...).populdate is not a function

[0]     at D:\공부\React\New_Boilerplate-master\server\routes\users.js:140:18

[0]     at D:\공부\React\New_Boilerplate-master\node_modules\mongoose\lib\model.js:4824:16

[0]     at D:\공부\React\New_Boilerplate-master\node_modules\mongoose\lib\model.js:4824:16

[0]     at D:\공부\React\New_Boilerplate-master\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16

[0]     at D:\공부\React\New_Boilerplate-master\node_modules\mongoose\lib\model.js:4847:21

[0]     at D:\공부\React\New_Boilerplate-master\node_modules\mongoose\lib\query.js:4390:11

[0]     at D:\공부\React\New_Boilerplate-master\node_modules\kareem\index.js:135:16

[0]     at processTicksAndRejections (internal/process/task_queues.js:75:11)

[0] Emitted 'error' event on Function instance at:

[0]     at D:\공부\React\New_Boilerplate-master\node_modules\mongoose\lib\model.js:4826:13

[0]     at D:\공부\React\New_Boilerplate-master\node_modules\mongoose\lib\helpers\promiseOrCallback.js:24:16

[0]     [... lines matching original stack trace ...]

[0]     at processTicksAndRejections (internal/process/task_queues.js:75:11)

[1] [HPM] Error occurred while trying to proxy request /api/users/removeFromCart?id=600bce9df829516a84314444 from localhost:3000 to http://localhost:5000 (ECONNRESET) (https://nodejs.org/api/errors.html#errors_common_system_errors)

const express = require('express');
const router = express.Router();
const { User } = require("../models/User");
const { Product } = require("../models/Product");
const { auth } = require("../middleware/auth");

//=================================
//             User
//=================================

router.get("/auth"auth, (reqres=> {
    res.status(200).json({
        _id: req.user._id,
        isAdmin: req.user.role === 0 ? false : true,
        isAuth: true,
        email: req.user.email,
        name: req.user.name,
        lastname: req.user.lastname,
        role: req.user.role,
        image: req.user.image,
        cart: req.user.cart,
        history: req.user.history,
    });
});

router.post("/register", (reqres=> {

    const user = new User(req.body);

    user.save((errdoc=> {
        if (errreturn res.json({ success: falseerr });
        return res.status(200).json({
            success: true
        });
    });
});

router.post("/login", (reqres=> {
    User.findOne({ email: req.body.email }, (erruser=> {
        if (!user)
            return res.json({
                loginSuccess: false,
                message: "Auth failed, email not found"
            });

        user.comparePassword(req.body.password, (errisMatch=> {
            if (!isMatch)
                return res.json({ loginSuccess: falsemessage: "Wrong password" });

            user.generateToken((erruser=> {
                if (errreturn res.status(400).send(err);
                res.cookie("w_authExp"user.tokenExp);
                res
                    .cookie("w_auth"user.token)
                    .status(200)
                    .json({
                        loginSuccess: trueuserId: user._id
                    });
            });
        });
    });
});

router.get("/logout"auth, (reqres=> {
    User.findOneAndUpdate({ _id: req.user._id }, { token: ""tokenExp: "" }, (errdoc=> {
        if (errreturn res.json({ success: falseerr });
        return res.status(200).send({
            success: true
        });
    });
});

router.post("/addToCart"auth, (reqres=> {
    // 먼저 User Collection에 해당 유저의 정보를 가져오기

    User.findOne({ _id: req.user._id},
        (erruserInfo=> {

            // 가져온 정보에서 카트에다 넣으려 하는 상품이 이미 들어 있는지 확인
            let duplicate = false;
            userInfo.cart.forEach((item=> {
                if(item.id === req.body.productId) {
                    duplicate = true;
                }
            })    

            if(duplicate) {
                
                User.findOneAndUpdate(
                    { _id: req.user._id"cart.id": req.body.productId }, 
                    { $inc: {"cart.$.quantity": 1} }, 
                    { new: true }, 
                    (erruserInfo=> {
                        if(errreturn res.status(400).json({ success: falseerr })
                        res.status(200).send(userInfo.cart)
                    }
                )
            } else {
                User.findOneAndUpdate(
                    { _id: req.user._id },
                    { 
                        $push: {
                            cart: {
                                id: req.body.productId,
                                quantity: 1,
                                date: Date.now()
                            }
                        }
                    },
                    { new: true },
                    (erruserInfo=> {
                        if(errreturn res.status(400).json({ success: falseerr })
                        res.status(200).send(userInfo.cart)
                    }
                )
            }

        })
});

router.get('/removeFromCart'auth, (reqres=> {
    // 먼저 카트 안에 내가 지우려고 한 상품을 지워주기
    User.findOneAndUpdate(
        {_id: req.user._id},
        {
            "$pull":
                { "cart": {"id": req.query.id} }
        },
        { new: true },
        (err,userInfo=> {
            let cart = userInfo.cart;
            let array = cart.map(item => {
                return item.id
            })


            // product collection에서 현재 남아 있는 상품들의 정보를 가져오기

            Product.find({ _id: { $in: array } })
                .populdate('writer')
                .exec((errproductInfo=> {
                    return res.status(200).json({
                        productInfo,
                        cart
                    })
                })
        }
    )


})

module.exports = router;

에러 전문입니다 살려주세요

답변 1

답변을 작성해보세요.

0

안녕하세요 껌거슨님   

보니깐populdate

이라고 작성하셨는데   populate   으로 바꿔주시면 됩니다 ~ !