Getting a typeerror 'client' undefined

Hello @taylordowns2000

I have an array of data for a particular question in my Kobo form.
The code below is similar to my code of what I want to do:

alterState(state =>{
var array = state.data.body[‘persondetails’];
array.forEach(data =>{
upsert({
‘table name’,
‘unique id’,
{
name: data[“first_name”],
surname: data[“surname”],
}

})
})
})

After running this code, I keep getting a:
TypeError: Cannot read property ‘client’ of undefined

If I console.log the data[“first_name”], its prints the persons name so I know that is working. I just don’t understand where the error keeps coming from.

Hrmmmm. Hard to tell what you’d like the outcome to be. Any chance you can share a formatted bit of the kobo submission (initial state) and the expression?

Assuming you mean you’ve got something like this as the initial state:

{
  "data": {
    "body": {
      "persondetails": [
        { "first_name": "taylor", "surname": "downs" },
        { "first_name": "brendon", "surname": "GS" },
        { "first_name": "mamadou", "surname": "lakhassane" }
      ]
    }
  }
}

and you want to create a row in your DB for each person in that persondetails array you could either use each and upsert like this:

each(
  dataPath('body.persondetails'),
  upsert('table name', 'unique id', {
    name: dataValue('first_name'), surname: dataValue('surname')
  })
);

or use the upsertMany function from the postgres adaptor like this:

upsertMany(
  'table name',
  'unique id',
  state => state.data.body.persondetails.map(i => ({
    name: i.first_name,
    surname: i.surname,
  }))
);

Both of these should work. Hope I’m understanding the intention, though!

Hello,

This is my initial state:

{
  "data": {
    "body": {
      "persondetails/people": [
        { "person/first_name": "taylor", "person/surname": "downs" },
        { "person/first_name": "brendon", "person/surname": "GS" },
        { "person/first_name": "mamadou", "person/surname": "lakhassane" }
      ]
    }
  }
}

When I implement the each function like this:

each(
  dataPath('body.persondetails/people'),
  upsert('table name', 'unique id', {
    name: dataValue('person/first_name'), surname: dataValue('person/surname')
  })
);

It upserts into my database but the values are null. So its not reading the values.

Any thoughts on why?

Ack! @brendonGS , I did a quick search for “each” in the OpenFn docs and found that the arrayPath there is wrong in my example. Sorry about that.

I think you’ll be all set if you change dataPath('body.persondetails/people'), to dataPath('body.persondetails/people[*]'), — note the [*].

1 Like

Thanks, that is what I was missing. It works now.

1 Like