Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript Object-Oriented JavaScript Object Basics Changing and Adding Properties

Murilo de Melo
Murilo de Melo
14,456 Points

Do properties created on the fly to objects become persistent in the DB or just accessible while the program is running?

I've seen many examples of properties created after the object has been created, dynamically. But It's not clear to me whether this can be made persistent for later access or can only be used while the program is running.

Tnx

1 Answer

Hi Murilo. Let's say that you create a new user like this:

db.create({
    email: req.body.email,
    username: req.body.name
}, (err, user) => {
    console.log(user);
});

Maybe you add a new property on this user object like this:

user.age = 31;

The user object will not be saved until you update the object with another method like this:

db.updateUser(user, (err, updatedUser) => {
    console.log('User updated.');
});

You can create your own custom methods (functions) to update/delete/create users using MongoDB.