These are chat archives for Automattic/mongoose

25th
May 2016
Vasiliy Onoprienko
@Regnised
May 25 2016 06:19
I am here
James Lee
@uptownhr
May 25 2016 06:25
i am here too
Ven Korolev
@ErrorPro
May 25 2016 06:43
me either
Rakesh Kashyap
@rakesh1988
May 25 2016 07:08
Hi All. I am a total newbie to mongoose and mongodb. Please Bear with me.. I was wiring node , express and mongodb for writing a rest service. i have placed my mongoose models in separate files. and my connection to db happens as soon as the application starts. when i try to execute "model.findone()" i get this error "TypeError: model.findOne is not a function" is it because i am creating my models before connecting to db
James Lee
@uptownhr
May 25 2016 07:09
u need to require your model file
Ven Korolev
@ErrorPro
May 25 2016 07:09
Are you sure that your model is mongoose model?
It should be mongoose Schema
James Lee
@uptownhr
May 25 2016 07:09
and your model file needs to export the model
checkout the project, hackathon-starter
Ven Korolev
@ErrorPro
May 25 2016 07:09
and you need to export it
James Lee
@uptownhr
May 25 2016 07:10
you can see a working boiler plate project
Rakesh Kashyap
@rakesh1988
May 25 2016 07:10
yes it is.
```
oops
var mongoose = require('mongoose');
var Schema = mongoose.Schema;

var userSchema = new Schema({
    email: String,
    password: {type: String, default: ''},
    firstName: String,
    middleName: String,
    lastName: String,
    oauthProvider: String,
    oauthProviderId: String,
    gender: String,
    createdDate: {type: Date, default: Date.now},
    updatedDate: {type: Date, default: Date.now},
    rawDataFromOauth: {type: Array}
});
var user = mongoose.model('user', userSchema);

module.exports = {
    user: user
};
James Lee
@uptownhr
May 25 2016 07:11
now your require script?

you should change your export to

module.exports = user

probably the problem
then you can do `var User = require('./model_file')
Ven Korolev
@ErrorPro
May 25 2016 07:11
You file has to export a function
not an object
James Lee
@uptownhr
May 25 2016 07:12
and then use like, User.findOne()
var User = require('./models/User')
User.findOne()
Rakesh Kashyap
@rakesh1988
May 25 2016 07:14
var userModel = require('./../../../model/user').user;
i was doing something like this
James Lee
@uptownhr
May 25 2016 07:14
that should work too
did you do, userModel.findOne() ?
Rakesh Kashyap
@rakesh1988
May 25 2016 07:14
            var model = new userModel(userDetailsFromPassport);

            model.findOne({email: model.email}, function (err, doc) {
James Lee
@uptownhr
May 25 2016 07:15
so
in this case
model is actually a document
not a query object
.findOne exists on the model
your model right now, is actually a document not a model
Rakesh Kashyap
@rakesh1988
May 25 2016 07:16
i am still in the object world.. trying hard to visualize javascript world :(
James Lee
@uptownhr
May 25 2016 07:16
what framework are you familiar with?
coming from php or ruby?
Rakesh Kashyap
@rakesh1988
May 25 2016 07:17
no i am from java and c#
James Lee
@uptownhr
May 25 2016 07:17
ah ic
but yea, if you just use your userModel, .findOne is available
Rakesh Kashyap
@rakesh1988
May 25 2016 07:17
im finding it really hard to get hold of javascript.. i visulise everything as an object.
James Lee
@uptownhr
May 25 2016 07:18
userModel is an object
Rakesh Kashyap
@rakesh1988
May 25 2016 07:18
so on which object does findone act on?
James Lee
@uptownhr
May 25 2016 07:18
and has .find and findOne as methods
on the model object
so for example
var User = require('./model/User') //model

app.get('/create-user', function(req,res){
  var user = new User({username: 'asdf'}) // document
  user.save(function(err,saved){
    res.send(saved)
  })
})

-------- on a separate request
app.get('get-user', function(req,res){
  User.findOne({username:'asdf'}, function(err, user){
    res.send(user)
  })
})
Rakesh Kashyap
@rakesh1988
May 25 2016 07:22
hmmm. this makes sense
James Lee
@uptownhr
May 25 2016 07:23
are you familiar with the async paradigm in js?
Rakesh Kashyap
@rakesh1988
May 25 2016 07:24
you are referring to how the callback works right?
James Lee
@uptownhr
May 25 2016 07:24
my example is not entirely correct for create-user, but works in this case. res.send will trigger even before save has completed. if you wanted res.send to come out after it actually saves, it'll need to be in the callback of save
yea
Rakesh Kashyap
@rakesh1988
May 25 2016 07:24
how the function() is called after execution of the request in db
James Lee
@uptownhr
May 25 2016 07:26
updated
Rakesh Kashyap
@rakesh1988
May 25 2016 07:26
i am really thankful for the pointers.
James Lee
@uptownhr
May 25 2016 07:26
mongoose also has a promise interface, if you familiar with it
np
anytime
user.save().then( function(user){} )
Rakesh Kashyap
@rakesh1988
May 25 2016 07:27
oh! what does then() do?
James Lee
@uptownhr
May 25 2016 07:27
user.findone({username:'asdf'}).then( function(user){} )
you should look up Promises
Rakesh Kashyap
@rakesh1988
May 25 2016 07:28
ill have a look at it now
James Lee
@uptownhr
May 25 2016 07:28
its a pattern in js
to handle async
but if you are just getting started, you should still master callbacks
you will not be able to avoid it honestly
also checkout a project i have, uptownhr/hackable
Rakesh Kashyap
@rakesh1988
May 25 2016 07:29
so that's number 1 in my to do list for today.
James Lee
@uptownhr
May 25 2016 07:30
its based on something called koajs
framework that is similiar to express
but built with the new es2015 async/await feature
Rakesh Kashyap
@rakesh1988
May 25 2016 07:31
wow! this also support login via fb and google
James Lee
@uptownhr
May 25 2016 07:31
yea
Rakesh Kashyap
@rakesh1988
May 25 2016 07:32
but, does it also support mobile way of authentication? because when we use mobile SDK's we already have JWT or access token
hackable looks like more of web based
pls correct me if im wrng
James Lee
@uptownhr
May 25 2016 07:33
yea it is
but you can build in tokens if you wanted
Rakesh Kashyap
@rakesh1988
May 25 2016 07:34
i should try this
James Lee
@uptownhr
May 25 2016 07:34
you can have a middleware that looks for token header
Rakesh Kashyap
@rakesh1988
May 25 2016 07:36
by the way my issue is fixed :clap:
James Lee
@uptownhr
May 25 2016 07:36
:+1:
Rakesh Kashyap
@rakesh1988
May 25 2016 15:08
I came across this plugin for mongoose called supergoose. (https://github.com/jamplify/supergoose) I am trying use this method findOrCreate() ... it takes an argument called [doc] . am i calling this method the right way?
 var model = new userModel(req.user);
 userModel.findOrCreate({email: req.user.email}, model, function (err, user)
whenever the document is not available, it creates a new doc. But what i see in db is totally different. i see only one key value pair, the one which i used for find. in this case email.