I am using following environment
NodeJS: 5.7.1
Mongo DB: 3.2.3
MongoDB (NodeJS Driver): 2.1.18
TypeScript: 1.8
I have created an Object using Typescript as
class User {
private _name:string;
private _email:string;
public get name():string{
return this._name;
}
public set name(val:string){
this._name = val;
}
public get email():string{
return this._email;
}
public set email(val:string){
this._email = val;
}
}
Using mongodb driver API, I am trying to insert object as
var user:User = new User();
user.name = "Foo";
user.email = "foo@bar.com";
db.collection('users').insertOne(user)
.then(function(r){..}).catch(function(e){..});
When I query from mongo console, to check the values inserted, using
db.users.find({}).pretty();
it gives me following output.
{
"_name":"Foo",
"_email":"foo@bar.com",
"name":"Foo",
"email":"foo@bar.com"
}
Why the private variables are getting stored? How can I prevent it from storing private variables.
EDIT: 1
Since, I couldn't stop developing the application, I have used a workaround for the time being. The domain object now has an additional method toJSON
which provides the structure, which I wish to store in MongoDB.
e.g.
public toJSON():any{
return {
"name":this.name
...//Rest of the properties.
};
}
I am calling toJSON()
on composed object as well.