Skip to content

Instantly share code, notes, and snippets.

View iMohamedSamir's full-sized avatar

Mohamed Samir iMohamedSamir

View GitHub Profile
@iMohamedSamir
iMohamedSamir / client\components\PostForm.js
Last active June 28, 2022 15:02
Solving: Uncaught (in promise) Error: Cannot assign to read only property 'getPosts'. following freeCodeCamp.org's tutorial: React / GraphQL Course - Build a social media app (MERNG Stack) @4:05:00
/*
* When following freeCodeCamp.org's youtube video tutorial: React / GraphQL Course - Build a social media app (MERNG Stack)
* I have got stuck @4:05:00 because I got an error that says:
* Uncaught (in promise) Error: Cannot assign to read only property 'getPosts' of object '#<Object>'
* that is happening because in apollo-client 3.6 (which I am using) immutability became mandatory, so, we can't assign new key
* to existing variable anymore like this: data.getPost = [resault.data.createPost, data.getPosts], but the solution is very simple:
*/
// instead of:
data.getPost = [resault.data.createPost, data.getPosts]
@iMohamedSamir
iMohamedSamir / posts.js
Last active April 9, 2022 02:03
Solving: Cast to ObjectId failed for value "" at path "_id". following freeCodeCamp.org's tutorial: React / GraphQL Course - Build a social media app (MERNG Stack) at 1:18:00
/*
* When following freeCodeCamp.org's youtube video tutorial: React / GraphQL Course - Build a social media app (MERNG Stack)
* I have got stuck @1:18:00 because I got a weired error: Cast to ObjectId failed for value "" at path "_id".
* It turn out that that ModelName.findById() method only expect vaild mongodb ID, so all I did is
* to use Types.ObjectId.isValid from mongoose to check if the given string is vaild id or not, if not,
* Skipped making the query and I threw new "Post not found" Error.
*/
import mongoose from 'mongoose';