How do I get the value of a key in the same object

Say I want callObj to equal blog.

This Repo is where im trying to implement it

const db = {
	blog: {
		name: 'dominic',
		age: 33,
		callObj: this.blog
	}
}

console.log(db.blog.callObj) // return {name: ..., age: ..., callObj: ...}

I believe that you’ll want to make callObj an object method to return this.

const db = {
	blog: {
		name: 'dominic',
		age: 33,
		getCallObj () {return this}
	}
}

console.log(db.blog.getCallObj())// return {name: ..., age: ..., callObj: ...}
1 Like

how would I use it inside this controller method. i want to add blog so that i can use blog.model and blog.connection inside the dbcontroller object

const database = {
    blog: {
        model: require('./model/blog_model'),
        connection: new Sequelize(config.blog),
        controller: new DbController(blog)
    }
};

This is starting to get into weird enough code gymnastics that I suspect you probably want to structure things differently than you are doing.

as @alkapwn3d said, I think you could initialize the DbConroller with this:

const database = {
    blog: {
        model: require('./model/blog_model'),
        connection: new Sequelize(config.blog),
        controller: new DbController(this)
    }
};

but i you can simplifie things and just put the connection and model in to seprate js files and then require them in the controller

@ArielLeslie
well call me kerri strug at the 1996 olympic gymnastics finals cos im about to break my own ankles

const Sequelize = require('sequelize');
const DbController = require('./controller/database_controller');
const config = require('./database/database_config');

const db = {
    blog: {
        model: require('./model/blog_model'),
        connection: new Sequelize(config.blog),
        controller() {
            return new DbController(this);
        }
    }
};

const data = {
    title: 'First entry',
    body: 'Your first blog entry was successful'
};

const Blog = db.blog.controller();
Blog.create_entry(data);
// Blog.find_all_entries()

Using a recursive call is pretty confusing: it would be much clearer to just pass an object with the two things (connection and model). And if you’re initialising the whole thing, you want to be as explicit as possible regarding the config object that gets passed in to avoid confusion, rather that doing that implicitly as happens here. Also, use a class! That’s what they’re there for, you can create a nice API and hide away the DB stuff inside it if you want, eg

const data = {
    title: 'First entry',
    body: 'Your first blog entry was successful'
};

const alkapwn3dBlog = new Blog(// some config here);
alkapwn3dBlog.createEntry(data);
2 Likes

was trying too hard to make it more readable but was actually more confusing with the same amount of lines