These are chat archives for Automattic/mongoose

9th
Aug 2017
Paul "Joey" Clark
@joeytwiddle
Aug 09 2017 02:47
@Faltek you can leave out the _id: Schema.Types.ObjectId, because that's the default.
But your actual problem is a typo: teacher in your schema, teacherId in your document. :P
If you want to be lazy, you can just submit a String instead of an ObjectId:
   teacher: teacherId,
   student: studentId,

@tylfin Your can increase the timeout time in your connection options. I think mongoose will keep trying to connect and submit your find when it does. (Except for the very first connection, which acts differently.)

You might also like to investigate why your MongoDB is not responding. (In our case, we were firing many heavy aggregations in parallel, which proved too heavy for MongoDB, so it started timing out on other queries too.)

Paul "Joey" Clark
@joeytwiddle
Aug 09 2017 02:58

@relentless-coder I always create schemas with strict: 'throw' so that I see errors when I submit bad queries.

You should also put a .catch() at the end of every promise, e.g. .catch(console.error) (or alternatively add an unhandledRejection handler to the process).

I don't know how to do your query but I suspect the find can only find a document (it cannot find an item inside an array inside a document) and so some of the complexity will move from there into your $set.

You might find it easier to load the User, modify it, and then save it again. :)

Alternatively, keep chats in their own collection.

Alexander
@Faltek
Aug 09 2017 06:30
@joeytwiddle Oh. Thank you very much!! Need to be more attentive :)
Quincy Koots
@qkoots
Aug 09 2017 07:44
Hello, I would like to know why the ES6 function syntax won’t work?
const mongoose = require("mongoose"),
      Schema = mongoose.Schema;

// Create schema and model
const newUserProfileSchema = new Schema(
    {
        firstName : String,
        lastName :  String,
        email :     String,
        password :  String,
        gender :    String,
        //birthDay :  Number,
        //birthMonth: String,
        //birthYear : Number
    }
);

// ES5: this works!!
newUserProfileSchema.methods.fullName = function () {
    let greeting = this.firstName
        ? "Meow name is " + this.firstName
        : "I don't have a name";
    console.log(greeting);
;


// ES2015:  This doesn't work
newUserProfileSchema.methods.fullName = () => {
    let greeting = this.firstName
        ? "Meow firstName is " + this.firstName
        : "I don't have a firstName";
    console.log(greeting);
};


let newUserProfile = new User({
    firstName : req.body.firstName,
    lastName  : req.body.lastName,
    email     : req.body.email,
    password  : req.body.password,
    gender    : req.body.gender,
});


newUserProfile.fullName();
Paul "Joey" Clark
@joeytwiddle
Aug 09 2017 07:46

@qkoots in an arrow function, this and arguments are not provided.

As a result, if you access them, you will be seeing the this or the arguments of their parent function.

So if you want to use this in the traditional way, then you have to use a function.

Quincy Koots
@qkoots
Aug 09 2017 07:50
@joeytwiddle So if I want to use this in a traditional way I need to use the ES5 syntax?
Paul "Joey" Clark
@joeytwiddle
Aug 09 2017 07:50
Yes

Another thing I would recommend, is to use const everywhere and let only on those rare occasions when you need it (when you need to re-assign a new value later).

It makes it easier to reason about a piece of code if it is clear that some of the values will never change.

Quincy Koots
@qkoots
Aug 09 2017 07:53
@joeytwiddle Okay, I will do some reading on the arrow functions.
@joeytwiddle Thank for the recommendation. I will keep that in mind.. Do you happen to have any good links about arrow functions? MDN is not always very handy for me about certain topics
charlie
@gabrielghost
Aug 09 2017 15:33
Hey all - looking into how to work with aliases in my schema
I see the feature has been added but can't find the instructions in the docs
anyone come across this?
charlie
@gabrielghost
Aug 09 2017 15:51
Automattic/mongoose@b01d955
found it