Siddhesh
Siddhesh

Reputation: 73

I am getting this error: Cannot read property 'likes' of null

I am getting error on console that : Cannot read property 'likes' of null I am using postman for getting requests and putting response and response. The array 'likes' is empty and here I am trying to insert the user id inside it but unable to insert it through unshift() method.

This is schema defined in a file Posts.js

const { text } = require('express');
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const PostSchema = new Schema({ 
    user: {
        type: Schema.Types.ObjectId,
        ref: 'users'
    },
    text: {
        type: String,
        required: true
    },
    name: {
        type: String
    },
    avatar: {
        type: String
    },
    likes: [
        {
            users: {
                type: Schema.Types.ObjectId,
                ref: 'users'
            }
        }
    ],
    comment: [
        {
            users: {
                type: Schema.Types.ObjectId,
                ref: 'users'
            },
            text: {
                type: String,
                required: true
            },
            name: {
                type: String,
            },
            avatar: {
                type: String
            },
            date: {
                type: Date,
                default: Date.now
            }
        }
    ],
    date: {
        type: Date,
        default: Date.now
    }
});

module.exports = Post = mongoose.model('post', PostSchema);

This is a express code for put request in file posts.js

const express = require('express');
const router = express.Router();
const { check, validationResult } = require('express-validator/check');
const auth = require('../../middleware/auth');
const Posts = require('../../models/Posts');
const User = require('../../models/User');
const { route } = require('./profile');

router.put('/like/:id', auth, async(req, res) => { 
    try {
        const post = await Post.findById(req.params.id);

        // Check if the post has already been liked
        if(post.likes.filter(like => like.user.toString() === req.user.id).length > 0) {
            return res.status(400).json({ msg: 'Post already liked' });
        }

        post.likes.unshift({ user: req.user.id });

        await post.save();

        res.json(post.likes);
    } catch (err) {
        console.error(err.message);
        res.status(500).send('Server Error');
    }
});

Upvotes: 1

Views: 73

Answers (1)

Siddhesh
Siddhesh

Reputation: 73

Here is the typo error I made. In the schema Posts.js in likes and comment array I wrote users instead of user.

const PostSchema = new Schema({ 
    user: {
        type: Schema.Types.ObjectId,
        ref: 'users'
    },
    text: {
        type: String,
        required: true
    },
    name: {
        type: String
    },
    avatar: {
        type: String
    },
    likes: [
        {
            users: {                        // Here it has to be user
                type: Schema.Types.ObjectId,
                ref: 'users'
            }
        }
    ],
    comment: [
        {
            users: {                           //Here it has to be user
                type: Schema.Types.ObjectId,
                ref: 'users'
            },

Upvotes: 1

Related Questions