Model.getAttributes is not a function

52 views Asked by At

I have an array of objects (docs) and a sequelize model(model) i'm building this objects into. The process looks something like this:

let mappedChunk = await Promise.all(
        docs.map(async (item) => {
            const pg_item: any = await model.build({
                _pg_key: uuidv4(),
                _source: JSON.stringify(item),
            }).dataValues

            await baseMapping(item, pg_item)

            if (extraMapping) {
                const status = await extraMapping(item, pg_item)
            }
            return pg_item
        }),
    )


await model.bulkCreate(mappedChunk, bulkCreateOptions)

baseMapping and extraMapping are just functions that pass data from attributes of those objects in the array to their corresponding attributes in sequelize models.

There's no problem here, the items get mapped into their corresponding pg_items.

However, when working on creating OneToMany and ManyToMany relations, i want to check if the foreign key even exists on the model like so:

const Model = pg_item.constructor

if (!(foreignKeyName in Model.getAttributes())) {...}

while executing the program just quits randomly saying "Model.getAttributes is not a function"

I've tried to console.log the pg_item, the constructor and the attributes by just using Object.keys(pg_item) to see if maybe some of the pg_items come in empty or even undefined (i don't even know how can they be undefined, but one can never be 100% sure i guess)

console.log('1')
console.log('pg_item <---------  ' + pg_item)
const Model = target.constructor
console.log('2')
console.log('Model <---------  ' + Model)

if (!(foreignKeyName in Model.getAttributes())) {...}

but the log just looks like this:


pg_item <---------  [object Object]
2
Model <---------  function Object() { [native code] }
1
pg_item <---------  [object Object]
2
Model <---------  function Object() { [native code] }
1
pg_item <---------  [object Object]
2
Model <---------  function Object() { [native code] }
TypeError: Model.getAttributes is not a function
    at /home/ulkash/Desktop/Cloned/mgn-sequelize/build/seeders/scripts/utils/relationsBinding.js:26:49
    at Generator.next (<anonymous>)

I understand that the "native code" means that the implementation of the Object function is written in a language other than JavaScript. Understandable.

However i just don't understand why at some point of execution it seems content with using this getAttributes() function on a sequelize object and then suddenly not so content anymore...

Do you have any suggestions on what i should do in this situation? Maybe some other checks to conduct? It's all very new to me and i'm just stumped... Thank you)

0

There are 0 answers