0

I am trying to understand why I'm getting a TypeError: ____.andWhereRaw is not a function using ObjectionJs with Postgres and how to resolve this error.

const { Model } = require('objection')

// Website Table
exports.up (knex, Promise) => {
 return knex.schema.createTable('web', table => {
   table.string('id');
   table.string('website');
   table.jsonb('other_sites').defaultTo('[]');
 }
}

exports.down .... // blah blah blah.....

*****************************************************************
// Website data:
{
  'id': 'orgIdA',
  'website': 'site1.com',
  'other_sites': ['foo.com', 'bar.com', 'xyz.com']
}

Class Website extends Model {
 const results = await GetModel // GetModel is a function "references"
 .query()
 .where('id', organizationId)
 .andWhere('website', website)

 const otherSite = results.map(site=> site= {...account.otherSites});


 if (otherSite) {
     Object.keys(otherSite).forEach(key => {
        results.andWhereRaw(`other_sites @> '{"${key}": "foo.com"}'`)
      });
 }

}

TypeError: results.andWhereRaw is not a function

Invisible-coder
  • 149
  • 1
  • 3
  • 12
  • Its really confusing but I would say `results` should be an array of Objection instances. `andWhereRaw` is a method of an Objection single instance – Rashomon Mar 21 '19 at 21:10
  • I think the function you meant is [whereRaw](https://vincit.github.io/objection.js/#whereraw), there is no such function as `andWhereRaw` – Gonzalo.- Mar 22 '19 at 02:49
  • @Rashomon Thank you. this was obvious. TYPE error, the data type being passed in. Adjusting my data to a single instance worked. – Invisible-coder Mar 22 '19 at 17:04
  • 1
    @Gonzalo.- you can use andWhereRaw which is similar to doing whereRaw such as .where() .andWhere(). Add raw just states you'll be writing sql syntax statements ('SELECT * FROM .... ') – Invisible-coder Mar 22 '19 at 17:05

0 Answers0