These are chat archives for Automattic/mongoose

9th
Nov 2017
Vlad
@vzelenko
Nov 09 2017 17:01
Hi, all - I'm trying to use Mongoose to model user with Different types of accounts: individual and corporate. Can I have a reference in UserSchema to Account, where Account is one of IndividualAccount || CorporateAccount types?
Arun Gadag
@arun-awnics
Nov 09 2017 17:46
You may have a property for the account type
Say, you have a AccountShema
Vlad
@vzelenko
Nov 09 2017 17:46
OK, let me clarify furhter.
User object:
Arun Gadag
@arun-awnics
Nov 09 2017 17:47
I am listening
Vlad
@vzelenko
Nov 09 2017 17:48
USER: {
id: “user-id”,
accountType: “individual | corporate”,
account: “some ref to another schema”
}
so, I’m storing accountType on the User schema.
Arun Gadag
@arun-awnics
Nov 09 2017 17:50
Is that your UserSchema?
Vlad
@vzelenko
Nov 09 2017 17:51
the relvant part.
there is also email and password hash, but that’s irrelevant
Arun Gadag
@arun-awnics
Nov 09 2017 17:52
You can have a reference like that
Vlad
@vzelenko
Nov 09 2017 18:06
sorry, did you say “You can or can’t?” @arun-awnics
Arun Gadag
@arun-awnics
Nov 09 2017 18:06
You can
Vlad
@vzelenko
Nov 09 2017 18:07
but my IndividualAccount and CorporateAccount schemas only share “userId” field
so, would I make them extend AccountSchema that defines it?
and put AccountSchema Type in “ref”?
Arun Gadag
@arun-awnics
Nov 09 2017 18:10
As I said before, you can have another field where you define accountType. By using that you can refer your schema
Vlad
@vzelenko
Nov 09 2017 18:11
I guess, I was looking for an example. It’s a bit abstract for me. Sorry...
Arun Gadag
@arun-awnics
Nov 09 2017 18:11
Alright...
userSchema: {
id: String,
accountType: String
}
You can pass accountType and use it in the Schema
Like...
accountSchema: {
...
}
Vlad
@vzelenko
Nov 09 2017 18:16
ok, so where does accountType go?
(THANKS!!!)
Arun Gadag
@arun-awnics
Nov 09 2017 18:19
Probably in a method through which you are trying to access accountSchema
Vlad
@vzelenko
Nov 09 2017 18:19
OK, so I guess I could NOT use a “ref” type to infer the schema.
Thanks.
really appreciate your help.
Arun Gadag
@arun-awnics
Nov 09 2017 18:23
Haha
Alright
You're welcome
tehstormcrow
@tehstormcrow
Nov 09 2017 18:45
Hi there, i wrote the following code, when i use node cats.js to run my file it starts an empty line (i can write in the line) and nothing really happens after that im using ubuntu
var mongoose = require("mongoose");
mongoose.connect("mongodb://localhost/cats", function(error){
    console.log("Problem" + error);
});
Vlad
@vzelenko
Nov 09 2017 18:53
Maybe, this could help?
// mongoose.connect(“conn string”);
var db = mongoose.connection;

//handle mongo error
db.on("error", logger.error);
db.once("open", () => logger.info("Mongo Connection Successful."));
tehstormcrow
@tehstormcrow
Nov 09 2017 18:56
its saying logger is undefined do i need to install npm pack for logger?
Vlad
@vzelenko
Nov 09 2017 18:57
sorry, feel free to replace it with console.log(“”)
so, logger.error becomes err => console.error(err)
and logger.info can be replaced with console.log
tehstormcrow
@tehstormcrow
Nov 09 2017 19:00
its says connection is succesfull, but it doesnt get me back to command system its just keep hanging there
Vlad
@vzelenko
Nov 09 2017 19:01
ah, but you must exit the application. Mongoose will open a connection thread and leave it running until you terminate your process, I think.
mongoose.disconnect() should do it.
tehstormcrow
@tehstormcrow
Nov 09 2017 19:03
k thank you very much, i was confused because at the class I'm taking his console returns back to command directly.
After mongoose.disconnect() it is returns to command now.
Vlad
@vzelenko
Nov 09 2017 19:04
:+1: