These are chat archives for Automattic/mongoose

2nd
Mar 2016
Daniel Acuña
@moustacheful
Mar 02 2016 02:18 UTC
anyone around?
dennisver
@dennisver
Mar 02 2016 02:18 UTC
Yea
Daniel Acuña
@moustacheful
Mar 02 2016 02:18 UTC
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 UTC
Wrong connection?
Yesterday I saw someone with the same issue
Daniel Acuña
@moustacheful
Mar 02 2016 02:20 UTC
the connection is fine, and no errors connecting either
dennisver
@dennisver
Mar 02 2016 02:20 UTC
What happens when you connect to another database?
Daniel Acuña
@moustacheful
Mar 02 2016 02:21 UTC
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 UTC
which repo?
Daniel Acuña
@moustacheful
Mar 02 2016 02:22 UTC
can't really point out, it's a private project
dennisver
@dennisver
Mar 02 2016 02:23 UTC
Oh ok
Daniel Acuña
@moustacheful
Mar 02 2016 02:24 UTC
did anything change with mongoose? i'm connecting with a mongo:// url
dennisver
@dennisver
Mar 02 2016 02:24 UTC
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 UTC
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 UTC
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 UTC
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 UTC
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 UTC
how would I go around that? I mean, there's no callbacks or anything coming from mongoose
dennisver
@dennisver
Mar 02 2016 02:29 UTC
What’s inside the mongoose obj?
Is there actually anything?
Daniel Acuña
@moustacheful
Mar 02 2016 02:29 UTC
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 UTC
What happens when you make an error yourself
Does it get thrown?
Daniel Acuña
@moustacheful
Mar 02 2016 02:32 UTC
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 UTC
What if you downgrade mongoose?
Of use another v
Daniel Acuña
@moustacheful
Mar 02 2016 02:33 UTC
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 UTC
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 UTC
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 UTC
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 UTC
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 UTC
yea, so you load that file
Daniel Acuña
@moustacheful
Mar 02 2016 02:44 UTC
yes
dennisver
@dennisver
Mar 02 2016 02:44 UTC
which intializes your model
Daniel Acuña
@moustacheful
Mar 02 2016 02:44 UTC
yep
and everything is fine, I even get model validation
dennisver
@dennisver
Mar 02 2016 02:44 UTC
and then you can access the model like const user = mongoose.model(‘User’)
user.save()
Daniel Acuña
@moustacheful
Mar 02 2016 02:44 UTC
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 UTC
:/
is mongodb running?
lol stupid question but you never know
Daniel Acuña
@moustacheful
Mar 02 2016 02:45 UTC
haha, yeah, one of the first things I tried
I have robomongo right now connected to it
dennisver
@dennisver
Mar 02 2016 02:45 UTC
haha ok :D
Daniel Acuña
@moustacheful
Mar 02 2016 02:46 UTC
I can put / pull stuff from there
no problem
dennisver
@dennisver
Mar 02 2016 02:46 UTC
what happens is you try raw mongodb queries in your app
instead of mongoose?
Daniel Acuña
@moustacheful
Mar 02 2016 02:50 UTC
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 UTC
how do you require the files
Daniel Acuña
@moustacheful
Mar 02 2016 02:51 UTC

User = require('./models/user');
var user = new User();
//...
nothing fancy about it
dennisver
@dennisver
Mar 02 2016 02:52 UTC
what happens is you don’t put it in a variable
just require it
Daniel Acuña
@moustacheful
Mar 02 2016 02:52 UTC
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 UTC
I’m out of inspiration haha
Daniel Acuña
@moustacheful
Mar 02 2016 02:54 UTC
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 UTC
Haha, good luck!
Daniel Acuña
@moustacheful
Mar 02 2016 02:55 UTC
thanks!
Jeff Lewis
@Jeff-Lewis
Mar 02 2016 03:32 UTC
@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 UTC
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 UTC
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 UTC
@uptownhr nice, it works too! thanks
J
@jhyland87
Mar 02 2016 15:12 UTC
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 UTC
return this.populate
or else the 2nd then doesnt work
J
@jhyland87
Mar 02 2016 15:44 UTC
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 UTC
but, not sure if the method populate returns a promise
J
@jhyland87
Mar 02 2016 15:46 UTC
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 UTC
check this out
so if you want a promise to be returned
need to use execPopulate
antonioaltamura
@antonioaltamura
Mar 02 2016 20:58 UTC
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 UTC
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 UTC
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 UTC

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. :)