Skip to content

odataclient.query is not a function in nodejs

I have function in my application which returns the student details by making an ODATA call. However the below code returns “this.edmOdataClient.query is not a function” error. value of Id that is passed to the function is 4B199,9h7dH,ATC3S,fDB5Y,h33Ny,kousB,lTibg,nuGM

Below is my code snippet

async getStudent(Id) {
    
        try {
            if (Id != undefined) {
                let index: number;
                for (index = 0; index < Id.length; index++) {
                    const element = Id[index];
                    console.log("ELEMENT" +element);
                    this.student = await this.OdataClient.get<any>
                        (
                            this.edmOdataClient
                                .query(`CD_STUDENT`)
                                .filter(new FilterClause("SECTION").eq("A"))
                                .andFilter(new FilterClause("ID").eq(element))
                                .select(["NAME", "GRADE"])
                                .orderBy("ID")
                                ).then(result => result.value[0])
                }
            }
            return this.student;
        }
        catch (error) {
            logger.info(error.message)
            return error;
        }

Also is there a way to check result.value.length? When am trying to do so am getting error that result is undefined

Answer

this is only something you can call if your function belongs to a class (also note that arrow functions dont respond to this unless you bind the function)

const externalFunc = () => {
  console.log('external func')
}

class MyClass {
  constructor () {
    this.externalFunc = externalFunc.bind(this)
  }

  myFuncOne () {
    console.log('func one')
  }
  
  myFuncTwo () {
    console.log('func two')
    this.myFuncOne()
  }
}

const klass = new MyClass()
klass.myFuncTwo()
// => func two
// => func one

klass.externalFunc()
// => external func

in your code, essentially the error is saying that getStudent is unable to reach edmOdataClient because its not bound to this

whatever class your code is running within doesnt have access to edmOdataClient