These are chat archives for Automattic/mongoose

2nd
Mar 2016
Daniel Acuña
@moustacheful
Mar 02 2016 02:18
anyone around?
dennisver
@dennisver
Mar 02 2016 02:18
Yea
Daniel Acuña
@moustacheful
Mar 02 2016 02:18
maybe you can help me :D
i'm having an issue where any database interaction I try I get nothing. no errors, no results, nothing.
any idea why that could be?
dennisver
@dennisver
Mar 02 2016 02:19
Wrong connection?
Yesterday I saw someone with the same issue
Daniel Acuña
@moustacheful
Mar 02 2016 02:20
the connection is fine, and no errors connecting either
dennisver
@dennisver
Mar 02 2016 02:20
What happens when you connect to another database?
Daniel Acuña
@moustacheful
Mar 02 2016 02:21
the same, tried on a local database and on a mlab database
I tried isolating components and it does work
but I just can't figure out where the problem is, and this repo was working a few months ago.
dennisver
@dennisver
Mar 02 2016 02:22
which repo?
Daniel Acuña
@moustacheful
Mar 02 2016 02:22
can't really point out, it's a private project
dennisver
@dennisver
Mar 02 2016 02:23
Oh ok
Daniel Acuña
@moustacheful
Mar 02 2016 02:24
did anything change with mongoose? i'm connecting with a mongo:// url
dennisver
@dennisver
Mar 02 2016 02:24
If you search the web for ‘Mongoose no error’, it happens a lot lol
Not afaik
Daniel Acuña
@moustacheful
Mar 02 2016 02:24
yeah! I saw that. something about not setting the models on the same connection
but I only have one, and tried logging the connection whenever a model was set and I got the same values all around
dennisver
@dennisver
Mar 02 2016 02:25
This sounds really weird
It’s really hard to help you find an error if you don’t get an error
Haha
Daniel Acuña
@moustacheful
Mar 02 2016 02:26
yeah, same reason why it's hard to debug for me :l
I'm not sure how to continue
that's why I'm asking in case someone else has gotten a similar problem
dennisver
@dennisver
Mar 02 2016 02:27
what if you use a middleware function to log errors in console
like app.use(function(err){console.log(err)) or something in your main file
Daniel Acuña
@moustacheful
Mar 02 2016 02:28
how would I go around that? I mean, there's no callbacks or anything coming from mongoose
dennisver
@dennisver
Mar 02 2016 02:29
What’s inside the mongoose obj?
Is there actually anything?
Daniel Acuña
@moustacheful
Mar 02 2016 02:29
yeah
it exists, same goes for the schema, model
so no errors as in 'can't call save() of undefined' or something like that
dennisver
@dennisver
Mar 02 2016 02:30
What happens when you make an error yourself
Does it get thrown?
Daniel Acuña
@moustacheful
Mar 02 2016 02:32
yup
tried that too, same with littering everything with console.logs
I'm just stumped
everything looks fine except for the fact that mongoose doesn't seem to be doing anything.
dennisver
@dennisver
Mar 02 2016 02:33
What if you downgrade mongoose?
Of use another v
Daniel Acuña
@moustacheful
Mar 02 2016 02:33
I was about to try that
trying 4.1.5, which was the one the project originally had installed and nothing
I'm going to try the models separately just to make sure they work
just a question, though, since I'm not sure I did this the right way
what's the right way to load a model? requiring the file (which exports the model) or using mongoose.model('Model') ?
dennisver
@dennisver
Mar 02 2016 02:40
mongoose.model(‘Model’) I guess
Depends what your file does
If you set your schema etc in that file, you probably first have to load that file
before you can access mongoose.model(‘Model’)
Daniel Acuña
@moustacheful
Mar 02 2016 02:41
yeah, I'm using requires so that souldn't be a problem.
it wasn't actually, until now. D:
dennisver
@dennisver
Mar 02 2016 02:42
so you’re first requiring a file which sets the model like mongoose.model(‘Model’, schema) & then access it somewhere else like mongoose.model(‘Model’) ?
Sounds fine
Daniel Acuña
@moustacheful
Mar 02 2016 02:43
no, for example I have models/user.js which has the schema definition and model declaration, which is exported as a module
as in: module.exports = mongoose.model('User',UserSchema)
dennisver
@dennisver
Mar 02 2016 02:43
yea, so you load that file
Daniel Acuña
@moustacheful
Mar 02 2016 02:44
yes
dennisver
@dennisver
Mar 02 2016 02:44
which intializes your model
Daniel Acuña
@moustacheful
Mar 02 2016 02:44
yep
and everything is fine, I even get model validation
dennisver
@dennisver
Mar 02 2016 02:44
and then you can access the model like const user = mongoose.model(‘User’)
user.save()
Daniel Acuña
@moustacheful
Mar 02 2016 02:44
yeah
and I get validation, It's just It doesn't seem to do anything when doing any kind of transaction with the actual database
dennisver
@dennisver
Mar 02 2016 02:45
:/
is mongodb running?
lol stupid question but you never know
Daniel Acuña
@moustacheful
Mar 02 2016 02:45
haha, yeah, one of the first things I tried
I have robomongo right now connected to it
dennisver
@dennisver
Mar 02 2016 02:45
haha ok :D
Daniel Acuña
@moustacheful
Mar 02 2016 02:46
I can put / pull stuff from there
no problem
dennisver
@dennisver
Mar 02 2016 02:46
what happens is you try raw mongodb queries in your app
instead of mongoose?
Daniel Acuña
@moustacheful
Mar 02 2016 02:50
uh... well I just tried using the models in a blank js file and they work
they save data and everything
dennisver
@dennisver
Mar 02 2016 02:50
how do you require the files
Daniel Acuña
@moustacheful
Mar 02 2016 02:51

User = require('./models/user');
var user = new User();
//...
nothing fancy about it
dennisver
@dennisver
Mar 02 2016 02:52
what happens is you don’t put it in a variable
just require it
Daniel Acuña
@moustacheful
Mar 02 2016 02:52
nothing would happen unless I swap to User = mongoose.model('User')
i'll try that anyway
yep, works both ways
dennisver
@dennisver
Mar 02 2016 02:54
I’m out of inspiration haha
Daniel Acuña
@moustacheful
Mar 02 2016 02:54
me too :/
thanks for the help though
I'm gonna keep trying
not like I have an option, haha
dennisver
@dennisver
Mar 02 2016 02:55
Haha, good luck!
Daniel Acuña
@moustacheful
Mar 02 2016 02:55
thanks!
Jeff Lewis
@Jeff-Lewis
Mar 02 2016 03:32
@dennisver take a look at https://github.com/sabymike/mongoose-relationship. Do read his note though in the readme.
James Lee
@uptownhr
Mar 02 2016 06:02
can you share more code?
@moustacheful most likely a connection issue from my experience
have you tried listening on the error event?
Loris Guignard
@loris
Mar 02 2016 14:39
hi guys, sorry for asking a question but i’m been looking everywhere in vain: when I update a document, I need to fill a property if another specific property has changed (for instance, if an API call change User.email I need to set User.emailStatus to "pending"). So I would need to compare the previous and the next value of the "email" property. I’ve been looking at the pre validate/save hooks to no avail.
Alright found it, didn’t know about path feature, that’s pretty rad
for anyone interested:
userSchema.path('email').set(function setEmail(newEmail) {
  if (this.email !== newEmail) {
    this.emailStatus = 'pending';
  }
});
Loris Guignard
@loris
Mar 02 2016 15:10
@uptownhr nice, it works too! thanks
J
@jhyland87
Mar 02 2016 15:12
Hey guys, im trying to populate documents inside a separate .then(), but it doesnt seem to work. I thought setting the Mongoose.Promise to the BluebirdJS promise object would help with that..
// app.js content..
const Mongoose = require('mongoose')
const Promise = require('bluebird')
Mongoose.Promise = Promise

// models/Foobar.js
// Single Mongoose model method
const Promise = require('bluebird')
partitionSchema.statics.getAssets = function( options, callback ) {
    return new Promise( ( res, rej ) => {
        const where = { /* .. filter stuff .. */ }

        Mongoose.models.Asset.find( where )
            .then( assetData => {
                if( _.isEmpty( assetData ) )
                    return rej( new Error( 'No documents found' ) )

                // Now populate ethe fields and resolve the assetDocs
                this.populate( assetData._partition, { path: '_fields' })
            })
            .then( assetData => {
                // Do some stuff with collection of populated documents
                res( assetData )
            })
            .catch( err => rej( _.setException( err ) ) )
    }).asCallback( callback )
}
Any ideas?

For now im just doing:

partitionSchema.statics.getAssets = function( options, callback ) {
    return new Promise( ( res, rej ) => {
        const where = { /* .. filter stuff .. */ }

        Mongoose.models.Asset.find( where )
            .then( assetData => {
                if( _.isEmpty( assetData ) )
                    return rej( new Error( 'No documents found' ) )

                // Now populate ethe fields and resolve the assetDocs
                this.populate( assetData._partition, { path: '_fields' }, ( err, result ) => {
                    if( err )
                        return rej( _.setException( err ) )

                    // Do some stuff with collection of populated documents
                    res( assetData )
                } )
            })
            .catch( err => rej( _.setException( err ) ) )
    }).asCallback( callback )
}

But id like to do it the other way, seems more "correct"

James Lee
@uptownhr
Mar 02 2016 15:43
return this.populate
or else the 2nd then doesnt work
J
@jhyland87
Mar 02 2016 15:44
Holy shit. I thought I did.. actually, I think I did, let me change it back and try it
James Lee
@uptownhr
Mar 02 2016 15:44
but, not sure if the method populate returns a promise
J
@jhyland87
Mar 02 2016 15:46
Ah, ok, so I actually did have it return the promise, just forgot to type that in the example above..
// app.js content..
const Mongoose = require('mongoose')
const Promise = require('bluebird')
Mongoose.Promise = Promise

// models/Foobar.js
// Single Mongoose model method
const Promise = require('bluebird')
partitionSchema.statics.getAssets = function( options, callback ) {
    return new Promise( ( res, rej ) => {
        const where = { /* .. filter stuff .. */ }

        Mongoose.models.Asset.find( where )
            .then( assetData => {
                if( _.isEmpty( assetData ) )
                    return rej( new Error( 'No documents found' ) )

                // Now populate ethe fields and resolve the assetDocs
                return this.populate( assetData._partition, { path: '_fields' })
            })
            .then( assetData => {
                // Do some stuff with collection of populated documents
                res( assetData )
            })
            .catch( err => rej( _.setException( err ) ) )
    }).asCallback( callback )
}
I tried using bluebirds promisify, but that didnt work, but I may have been doing it wrong, ill check it out
I figured the this.populate would return a promise if the Mongoose.Promise was set
James Lee
@uptownhr
Mar 02 2016 15:57
check this out
so if you want a promise to be returned
need to use execPopulate
antonioaltamura
@antonioaltamura
Mar 02 2016 20:58
guys could someone help me with this?
http://stackoverflow.com/questions/35750920/find-in-array-and-returns-that-element
Basically I have an array field, I want to make an "autocomplete" query as I explained..
antonioaltamura
@antonioaltamura
Mar 02 2016 21:04
in 2 words the problem above: a $filter with a $regex as condition, don't know why it's working
dennisver
@dennisver
Mar 02 2016 21:17
Does anyone know if there’s something in Schema definitions like role: String, value: in[‘admin’, ‘manager’, ‘user’]?
Renader
@Renader
Mar 02 2016 22:16

hey guys,

anyone has a good example for the correct use of mongoose with typescript? I mean with interfaces, with a class that contains all methods , ... ? I'm new to TS, Mongoose and OO but want to try it. :)