These are chat archives for Automattic/mongoose

9th
Nov 2017
Vlad
@vzelenko
Nov 09 2017 17:01 UTC
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 UTC
You may have a property for the account type
Say, you have a AccountShema
Vlad
@vzelenko
Nov 09 2017 17:46 UTC
OK, let me clarify furhter.
User object:
Arun Gadag
@arun-awnics
Nov 09 2017 17:47 UTC
I am listening
Vlad
@vzelenko
Nov 09 2017 17:48 UTC
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 UTC
Is that your UserSchema?
Vlad
@vzelenko
Nov 09 2017 17:51 UTC
the relvant part.
there is also email and password hash, but that’s irrelevant
Arun Gadag
@arun-awnics
Nov 09 2017 17:52 UTC
You can have a reference like that
Vlad
@vzelenko
Nov 09 2017 18:06 UTC
sorry, did you say “You can or can’t?” @arun-awnics
Arun Gadag
@arun-awnics
Nov 09 2017 18:06 UTC
You can
Vlad
@vzelenko
Nov 09 2017 18:07 UTC
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 UTC
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 UTC
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 UTC
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 UTC
ok, so where does accountType go?
(THANKS!!!)
Arun Gadag
@arun-awnics
Nov 09 2017 18:19 UTC
Probably in a method through which you are trying to access accountSchema
Vlad
@vzelenko
Nov 09 2017 18:19 UTC
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 UTC
Haha
Alright
You're welcome
tehstormcrow
@tehstormcrow
Nov 09 2017 18:45 UTC
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 UTC
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 UTC
its saying logger is undefined do i need to install npm pack for logger?
Vlad
@vzelenko
Nov 09 2017 18:57 UTC
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 UTC
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 UTC
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 UTC
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 UTC
:+1: