These are chat archives for Automattic/mongoose

12th
Jan 2018
Sreekar Reddy
@sreekar2307
Jan 12 2018 12:45
pls check the link
https://gist.github.com/sreekar2307/9262a74f3bedd3a669e10673f0fd22e6
error due to line 88 on app.js file
error info : UnhandledPromiseRejectionWarning: ValidationError
Kev
@lineus
Jan 12 2018 12:57
@sreekar2307 does console.log(coffeshop) on line 87 display anything before line 88 throws?
can you add a file to the gist with the err, it might make it easier to debug
Kev
@lineus
Jan 12 2018 13:06

maybe instead of:

Coffeshop.findById(req.params.id,function(err,coffeshop){

try:

Coffeshop.findById({ _id: req.params.id}, function(err,coffeshop){
Sreekar Reddy
@sreekar2307
Jan 12 2018 14:03
It didn't work
getting error on 88 line
for app.js
Sreekar Reddy
@sreekar2307
Jan 12 2018 16:37
Can any one help me plz i am kind of stuck here
Juha Lindstedt
@pakastin
Jan 12 2018 16:39
What does the schema look like? You’re getting a validation error, which means your data is invalid according to the schema
Sreekar Reddy
@sreekar2307
Jan 12 2018 16:40
var CoffeshopsSchema = new mongoose.Schema({// constructor parameters define how each row should be
name:String,
image:String,
description:String,
comments:[
{
type:mongoose.Schema.Types.ObjectId,
ref:"comments"
}
]
});
var CommentsSchema = new mongoose.Schema({
comment:String,
author:String
});
Sreekar Reddy
@sreekar2307
Jan 12 2018 17:11
Any solution?
Juha Lindstedt
@pakastin
Jan 12 2018 17:25
Try: coffeeshop.comments.push(comment._id) 😉
line 87
Sreekar Reddy
@sreekar2307
Jan 12 2018 17:30
nope didn't work
Juha Lindstedt
@pakastin
Jan 12 2018 17:31
😕
Sreekar Reddy
@sreekar2307
Jan 12 2018 17:48
i don't why this isn't working i used the same code for seed.js which worked
coffeshop.comments.push(comment);
Sreekar Reddy
@sreekar2307
Jan 12 2018 18:02
tell me another way to do this
Sreekar Reddy
@sreekar2307
Jan 12 2018 18:17
????????????????????????????
Juha Lindstedt
@pakastin
Jan 12 2018 18:33
What does console.log(req.body) say?
line 74
Sreekar Reddy
@sreekar2307
Jan 12 2018 18:41
that's working fine it fetches info from the form present in show.ejs
Juha Lindstedt
@pakastin
Jan 12 2018 18:50
I mean is the data in the right format according to the schema?
Sreekar Reddy
@sreekar2307
Jan 12 2018 19:05

This is what is returned from the form in console.log(req.body);

(this input is entered by me in coffeshops/:id route)
{ author: 'Sreekar',
comment: 'This is a wonderful location',
submit: 'Submit' }

Comment.create({comment:body.comment,author:body.author},function(err,comment){
}); (used this is to save in the comments collection)

this is my comment schema
var CommentsSchema = new mongoose.Schema({
comment:String,
author:String
});

Sreekar Reddy
@sreekar2307
Jan 12 2018 19:12
?????????????
Kev
@lineus
Jan 12 2018 19:18
@sreekar2307 can you please add the output from the console in the gist you created?
I have some time to take a look
Sreekar Reddy
@sreekar2307
Jan 12 2018 19:22
Screenshot (3).png
its even on the gist
Kev
@lineus
Jan 12 2018 19:27
in your coffeeshop schema file you set module.exports twice, that seems problematic.
looks like you might have accidentally added the comment schema to the coffeshop schema file
Sreekar Reddy
@sreekar2307
Jan 12 2018 19:33
well it actually got copied twice on the gist
but it is written only once on my original file
Kev
@lineus
Jan 12 2018 19:35
ah, OK. I'm setting it up in my env. give me a few and I'll see what I can figure out.
Sreekar Reddy
@sreekar2307
Jan 12 2018 20:08
anything?
Kev
@lineus
Jan 12 2018 20:26
yeah I think so..
your schema is setup to have a subdoc of refs to comments but in that spot you're pushing the whole comment onto the subdoc array
coffeshop.comments.push(comment)
try just pushing the _id
coffeshop.comments.push(comment._id)
Kev
@lineus
Jan 12 2018 20:31
I just added a comment to your gist with a find from my testdb that shows that worked for me
the other thing i noticed is in your post /coffeshops route, the res.redirect after the create call seems off to me. I think that is going to fire in the wrong order. I think that should be in the success condition of your last callback.
Kev
@lineus
Jan 12 2018 21:04
@sreekar2307 let me know if that helps or how it changes the output/error
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 22:58

One more question, and so it seems this course is full of them. When I go to reference a type in mongoose, it says that it is null. At first, I thought this was an issue with the attached code, where it was referencing, but then I checked it before the value is sent, and it says it is null. The weird thing about this is that before this, it was working fine, and I only had edited the app.js file. Any ideas?

This is the code:
null
null
TypeError: /Users/ryantopham/Desktop/CWDC/YelpCamp/views/show.ejs:5
3| <% console.log(campground) %>
4|

5| <h1><%= campground.name %></h1>
6|
7| <img src="<%= campground.image %>">
8|

Cannot read property 'name' of null

Or error I should say
This is the code that is surrounding the error

<% include partials/header %>

<% console.log(campground) %>

<h1><%= campground.name %></h1>

<img src="<%= campground.image %>">

<p><%= campground.description %></p>

<% include partials/footer %>

Kev
@lineus
Jan 12 2018 23:15
what change did you make to your app.js file?
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:17

I changed the app.js file function(app.get("/campgrounds/:id")) from this:

app.get("/campgrounds/:id", function(req, res) {
//Find the campground with provided ID
Campground.findById(req.params.id, function(err, foundCampground) {
if(err) {
console.log(err);
} else {
//Render the show template with that campground
res.render("show", {campground: foundCampground});
}
});
});

To what is seen on the gist. Starting at around like 68

line 68*
Kev
@lineus
Jan 12 2018 23:20
do you see anything in your browser console ( from the show.ejs call to console.log() ) ?
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:21

Only this error:

Refused to load the font 'data:font/woff;base64,d09GRgABAAAAAIw4ABEAAAABQcAAAQABAAAAAAAAAAAAAAAAAAAAAAAAAABHUE9TAAABgAAACU8AABMOD1XXMEdTVUIAAArQAAAAUwAAAHCOOI56T1MvMgAACyQAAABbAAAAYNrnrZBjbWFwAAALgAAAAIEAAAC0jBiQAGN2dCAAAAwEAAAAKgAAAC4HyBmgZnBnbQAADDAAAAOBAAAG5XJackBnYXNwAAAPtAAAAAwAAAAMAA0AGGdseWYAAA/AAABViQAAvQzAr6AGaGVhZAAAZUwAAAA2AAAANvzd8kdoaGVhAABlhAAAAB8AAAAkD/YHpGhtdHgAAGWkAAACFAAAA26BW0HVa2VybgAAZ7gAAB/3AABeRKoEucNsb2NhAACHsAAAAb4AAAG+1eip6m1heHAAAIlwAAAAIAAAACAByAf2bmFtZQAAiZAAAADEAAABYBoFNRJwb3N0AACKVAAAAZYAAAJBVvgNXnByZ...KKKqa4EkoqpbQyyiqnvAoqqqSyKqqqproaaqqltjrqqqe+BhpqpLEmmmqmuRZaaqW1Ntpqp70OOuqksy66BhE2m2Gms1Z5Z5ZF5ltrpy1BpHlemm65H35aaLU5Lnvru3V2+e2XPzbZ66br9ummuyV6uK2nG26574677nmvl0ceeGi/3r5Z6qnHnujjo8/m6qev/gYaYJANBhtqiGGGG2mEUUb7YIxxxhpvoglO2GiySaaY6pMvTnnmgIOee+OFQw475rgrjjjqqtnOOe9MEOWrCy4G0UFMEBvEBfFB+N9Y/EJ9fNhL8zINDBwNoLQLi2tpUT5XelFiWapecmJxKm9KZmpRanFmMZjHlZhcWgKR4E/OLEouzU3LSa0A8zmLMvPSIYpKMnNSIIoAYXqDQQAAuQgACABjILABI0QgsAMjcLAURSAgsChgZiCKVViwAiVhsAFFYyNisAIjRLMJCgMCK7MLEAMCK7MRFgMCK1myBCgGRVJEswsQBAIrAA==' because it violates the following Content Security Policy directive: "default-src 'self'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback.

5a593aa8c0f0c102ebb0b9a2 Failed to load resource: the server responded with a status of 500 (Internal Server Error)

Repeated about a hundred times. Though for the life of me, I cannot make sense of ti

it*
This is the error that I get when i launch the page, without actually having to go to the console in the browser

TypeError: /Users/ryantopham/Desktop/CWDC/YelpCamp/views/show.ejs:5
3| <% console.log(campground) %>
4|

5| <h1><%= campground.name %></h1>
6|
7| <img src="<%= campground.image %>">
8|

Cannot read property 'name' of null
at eval (eval at compile (/Users/ryantopham/Desktop/CWDC/YelpCamp/node_modules/ejs/lib/ejs.js:549:12), <anonymous>:25:37)
at returnedFn (/Users/ryantopham/Desktop/CWDC/YelpCamp/node_modules/ejs/lib/ejs.js:580:17)
at tryHandleCache (/Users/ryantopham/Desktop/CWDC/YelpCamp/node_modules/ejs/lib/ejs.js:223:34)
at View.exports.renderFile [as engine] (/Users/ryantopham/Desktop/CWDC/YelpCamp/node_modules/ejs/lib/ejs.js:437:10)
at View.render (/Users/ryantopham/Desktop/CWDC/YelpCamp/node_modules/express/lib/view.js:135:8)
at tryRender (/Users/ryantopham/Desktop/CWDC/YelpCamp/node_modules/express/lib/application.js:640:10)
at Function.render (/Users/ryantopham/Desktop/CWDC/YelpCamp/node_modules/express/lib/application.js:592:3)
at ServerResponse.render (/Users/ryantopham/Desktop/CWDC/YelpCamp/node_modules/express/lib/response.js:1008:7)
at /Users/ryantopham/Desktop/CWDC/YelpCamp/app.js:74:8
at /Users/ryantopham/Desktop/CWDC/YelpCamp/node_modules/mongoose/lib/query.js:3115:18
at newTickHandler (/Users/ryantopham/Desktop/CWDC/YelpCamp/node_modules/mpromise/lib/promise.js:234:18)
at _combinedTickCallback (internal/process/next_tick.js:131:7)
at process._tickCallback (internal/process/next_tick.js:180:9)

Kev
@lineus
Jan 12 2018 23:23
do you see the server side error(s)?
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:23
@lineus such as in the mongod terminal window?
This is the only error that I can gather from where I am running mongod
2018-01-12T18:13:08.134-0500 I NETWORK [conn16] received client metadata from 127.0.0.1:50351 conn: { application: { name: "MongoDB Shell" }, driver: { name: "MongoDB Internal Client", version: "3.6.0" }, os: { type: "Darwin", name: "Mac OS X", architecture: "x86_64", version: "17.3.0" } }
Kev
@lineus
Jan 12 2018 23:25
never mind that, I wasn't thinking about the fact that express might blow up on a view file that it doesn't like. I'll take a closer look at the show.ejs file and see if I can help.
in the window where you are running your express server, does it look happy or sad?
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:27
@lineus I am not sure where that could be found. is that the node app.js command wondow? If so, it is happy at first, until I go the address of the campgrounds/:id, in which case it turns to be a very unhappy server window
That sounded dumb. Took me a minute to figure it out. It does not look happy once I go to the address
Kev
@lineus
Jan 12 2018 23:28
are the errors in the node app.js window the same as the one that gets rendered in your view?
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:29
Exectly the same, however in the node app.js window, they are said 3 times, not ust once, which is extremely wierd to me. Since it terminates itself after it runs the third time and not the first...
Kev
@lineus
Jan 12 2018 23:29
how many campgrounds are you adding? 3?
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:30
Yes, I am adding three, but only viewing one with the specific id of the web adress
Kev
@lineus
Jan 12 2018 23:31
before you get the view, do you see any of the output from the console.log(foundCampground); that runs right before?
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:31
It says "null"
Kev
@lineus
Jan 12 2018 23:32
did you verify that the id you are searching for exists in the db?
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:33
Which s what I meant when I said that it sends a null object to the show.ejs file. When I check the database, it shows the exact id of what is in the address. I even tried copying and pasting it into the address

I am also now getting this error in the express server window

(node:1254) DeprecationWarning: Mongoose: mpromise (mongoose's default promise library) is deprecated, plug in your own promise library instead: http://mongoosejs.com/docs/promises.html

Kev
@lineus
Jan 12 2018 23:35

you can add

mongoose.Promise = global.Promise

to sort that out

right under where you require mongoose
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:36

Well, that changed some things… When I do that, I get the following error(s):

(node:1284) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): MongoError: Unknown modifier: $pushAll
(node:1284) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Created a new comment!
(node:1284) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): MongoError: Unknown modifier: $pushAll
(node:1284) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 3): MongoError: Unknown modifier: $pushAll

And then it terminates the server
Without me visiting the website at all
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:41
Managed to get rid of that by Commenting it out, restarting mongo, running without, restarting mongo, and then running with. But that went away now. But now it is saying exactly what it was before
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:48
I think I may have found the solution. I might have been using a saved keyword for the function, because when I changed the name f all of the variables respoectively, I was able to make it go away… not sure if this was my fault, or mongoose being confused… Thinking ti was me
Thank you for you rhelp though, @lineus
Kev
@lineus
Jan 12 2018 23:49
what do you mean by a saved keyword?
and no problem, this stuff is fun!
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:50
I thought that every language had a set of wordds that are built into their language, meaning that if you use them, it will confuse them with something else, meaning the code will not run properly. Though the compiler will not know that, because it thinks it exists. I don’t know the list of the ones that mongoose and express use, but I am willing to bet that was my problem
Kev
@lineus
Jan 12 2018 23:52
oh, reserved words, yeah I know what you mean. I didn't see any, do you know which word fixed it?
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:52
I am not sure, as I changed them all
Like a stupid developer, I made a ton of changes, without testing iterations… :(
Kev
@lineus
Jan 12 2018 23:53
at least you sorted it out!
AnGrYbEaRcOdE
@CodeME1118
Jan 12 2018 23:59
I would like to ask one more question. When I do this, all of the errors are gone, but the comments piece of my campgrounds schema is empty. I was having this issue before, and am not sure why this is. In the seeds.js file, I am making the compgrounds schemas(3 of them), and then if those work fine, I make 1 comment in each of those, but the output in the console.log is something that says the comments array is empty
Thank you for any help in advance