# Errors editing/updating existing documents in couchDB

**URL:** <https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968>\
**Category:** Development\
**Tags:** users\
**Created:** [June 29, 2022, 1:20pm UTC](https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968 "2022-06-29T13:20:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kymoraa](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/kymoraa/32/942_2.png) [@Kymoraa](https://forum.communityhealthtoolkit.org/u/Kymoraa)\
**Post date:** [June 29, 2022, 1:20pm UTC](https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968/1 "2022-06-29T13:20:06Z")

</div>

**Describe the Bug**  
For context, This is what I am hoping to achieve.  
I need to run a command to bulk update certain users’ couchDB documents with an additional flag so that they are able to have access to specific forms while those who don’t have the flag aren’t able to access the forms. This is because, Uganda is currently training their VHT members on family planning sequentially. Thus, when the training is completed, only those users need to view those forms and targets.  
@derick shared this helpful documentation on how to go about editing the contacts: [GitHub - medic/cht-conf: A command-line interface for configuring Community Health Toolkit applications](https://github.com/medic/cht-conf#editing-contacts-across-the-hierarchy)  
After following the steps detailed, I am running into the error below.

 ![Screenshot 2022-06-29 at 16.10.21](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/0/068fbb28a1be39842aef1628cf8844b43f89cf5c.png)

**How To Reproduce The Issue**  
1.Create a contact.csv file with your columns in the csv folder in your current path.  
(I am not able to attach the contacts.csv file. But here is a screenshot. I just have the mandatory column ‘documentID’ and I am testing with 1 UUID)

![Screenshot 2022-06-29 at 16.14.00](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/a/a540fcfaddd94d5be9444d1edbc60babaa44b65b.png)

1. Use the following command to download and edit the documents:

```auto
cht --url=https://<admin_user>:<password>@<instance> edit-contacts -- --docDirectoryPath=/Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya

```

**Environment**

- Browser: Chrome
- Client platform: MacOS
- Instance version: 3.13
- cht --version `INFO 3.11.0`

**Additional Info**  
When I edit the ‘edit-contacts.js’ to add a log statement on line 100 in order to see which columns are actually found.

```auto
 console.log("COLUMNS", cols);

```

```auto
INFO Processing CSV file: /Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya/csv/contact.csv … 
COLUMNS ['documentID']
INFO Error: missing "documentID" column.

```

---

<div class="post-metadata">

**Author:** ![gareth](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/gareth/32/62_2.png) [@gareth](https://forum.communityhealthtoolkit.org/u/gareth)\
**Post date:** [June 29, 2022, 10:00pm UTC](https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968/2 "2022-06-29T22:00:44Z")

</div>

Hi @Kymoraa

I’ve tried this out on my install and I can’t reproduce your error. What I did was…

```auto
cd ~/path-to-my-csv
cht edit-contacts --local

```

My CSV looks like:

```auto
documentID
123

```

The error I got was…

```auto
INFO Processing CSV file: /home/gareth/Desktop/csv/contact.csv … 
INFO Downloading doc(s)... 
INFO Error: Document with id '123' could not be found.

```

I wonder if there’s some parsing error on your CSV. Can you try my really simple example?

Secondly, can you check that cht-conf is loading the expected CSV and not finding a blank one instead?

---

<div class="post-metadata">

**Author:** ![Kymoraa](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/kymoraa/32/942_2.png) [@Kymoraa](https://forum.communityhealthtoolkit.org/u/Kymoraa)\
**Post date:** [June 30, 2022, 9:04am UTC](https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968/3 "2022-06-30T09:04:49Z")

</div>

Hey @gareth

Thanks for your reply  
I can see that you have changed directories to the path of the csv file. Is this the case? I am running the command from the root folder and appending the path to the csv.  
If I run the command from within the csv folder, I get the expected error that we don’t have the package.json …  
Unless, I am not supposed to have the csv file in a csv folder as stated here: [1. Create a contact.csv file with your columns in the csv folder in your current path.](https://github.com/medic/cht-conf#example)?

```auto
cht edit-contacts --url=https://medic:password@clone-smarthealth-ke-prod-3-13.lg-apps.com
WARN No project package.json file found at /Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya/csv/package.json 
WARN Project has no dependency on cht-conf. 
INFO Checking that https://medic: **** @clone-smarthealth-ke-prod-3-13.lg-apps.com/medic/ is available... 
INFO Processing config in csv. 
INFO Actions:
     - edit-contacts 
INFO Starting action: edit-contacts… 
WARN No csv directory found at /Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya/csv/csv. 
INFO edit-contacts complete. 

```

However, as much as i received the above error, something interesting happened. An empty folder **json\_docs** was created in the csv folder.  
And when I ran the same command from the root, the same empty file was created. But here I got the error `ERROR missing "documentID" column.`

 ![Screenshot 2022-06-30 at 12.03.57](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/d/deca50dae006829ad0cff858f0ca9b57d43ff759.png)

The second part:

> Secondly, can you check that cht-conf is loading the expected CSV and not finding a blank one instead?

How can I check this?

---

<div class="post-metadata">

**Author:** ![gareth](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/gareth/32/62_2.png) [@gareth](https://forum.communityhealthtoolkit.org/u/gareth)\
**Post date:** [June 30, 2022, 7:44pm UTC](https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968/4 "2022-06-30T19:44:50Z")

</div>

Ok I think we’re getting closer. You’ll see in your output this line:

```auto
WARN No csv directory found at /Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya/csv/csv.

```

Notice the directory has two `/csv` paths at the end. If you go up one level in the directory I think it’ll find the right file.

> How can I check this?

In your original console output there’s a line that starts with “INFO: Processing CSV file: …”. It’d be worth checking that the file at that path to make sure it’s the csv you expect, eg:

```auto
less /Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya/csv/contact.csv

```

---

<div class="post-metadata">

**Author:** ![Kymoraa](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/kymoraa/32/942_2.png) [@Kymoraa](https://forum.communityhealthtoolkit.org/u/Kymoraa)\
**Post date:** [July 1, 2022, 8:17am UTC](https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968/5 "2022-07-01T08:17:35Z")

</div>

Hello @gareth  
I don’t have two csv folders.

`WARN No csv directory found at /Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya/csv/csv.`

This error above is if I run the command when I cd into the csv folder… (which I saw you did in the example you shared)  
But I’m not sure if we are supposed to cd into that csv folder to run the command

So if I run the command at the root, the error I get is instead the one below. Where it finds the contact.csv file but the **column is missing**

```auto
config-lg-forked/config-lg-kenya [test-update-users●] » cht edit-contacts --url=https://medic:668dd65770d21ffe@clone-smarthealth-ke-prod-3-13.lg-apps.com
INFO Checking that https://medic: **** @clone-smarthealth-ke-prod-3-13.lg-apps.com/medic/ is available... 
INFO Processing config in config-lg-kenya. 
INFO Actions:
     - edit-contacts 
INFO Starting action: edit-contacts… 
INFO Processing CSV file: /Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya/csv/contact.csv … 
INFO Error: missing "documentID" column.
    at getIDs (/Users/jackiemoraa/.nvm/versions/node/v16.15.0/lib/node_modules/cht-conf/src/fn/edit-contacts.js:101:10)
    at /Users/jackiemoraa/.nvm/versions/node/v16.15.0/lib/node_modules/cht-conf/src/fn/edit-contacts.js:71:68
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async module.exports (/Users/jackiemoraa/.nvm/versions/node/v16.15.0/lib/node_modules/cht-conf/src/lib/main.js:210:5)
    at async /Users/jackiemoraa/.nvm/versions/node/v16.15.0/lib/node_modules/cht-conf/src/bin/index.js:16:5 
ERROR missing "documentID" column. 

```

I can confirm that this is indeed the path to the csv file

```auto
/Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya/csv/contact.csv

```

---

<div class="post-metadata">

**Author:** ![gareth](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/gareth/32/62_2.png) [@gareth](https://forum.communityhealthtoolkit.org/u/gareth)\
**Post date:** [July 3, 2022, 7:00pm UTC](https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968/6 "2022-07-03T19:00:21Z")

</div>

> But I’m not sure if we are supposed to cd into that csv folder to run the command

I used that in my testing to be sure it was getting to the right file and to simplify the command to eliminate other possible errors. Please run this test again, one directory higher, eg:

```auto
cd /Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya
cht edit-contacts --url=https://<admin_user>:<password>@<instance>

```

If you get the same error that will confirm that it’s attempting to read the same file.

Once you’ve done that, then it suggests that cht-conf cannot read the file. Try replacing the csv with a simple example, like the two line one I used:

```auto
documentID
123

```

This should error because the contact with ID `123` is not found but if you get that far then that proves the CSV is being parsed correctly. To create this new CSV I’d recommend just using a simple text editor, typing the contents, and saving as `contact.csv`.

---

<div class="post-metadata">

**Author:** ![Kymoraa](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/kymoraa/32/942_2.png) [@Kymoraa](https://forum.communityhealthtoolkit.org/u/Kymoraa)\
**Post date:** [July 4, 2022, 1:58pm UTC](https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968/7 "2022-07-04T13:58:04Z")

</div>

Hello @gareth  
a) If I run it one directory higher, I will be outside the folder containing the package.json and also, another error is not finding the .csv as well

```auto
WARN No project package.json file found at /Users/jackiemoraa/Documents/Work/config-lg-forked/package.json 
WARN Project has no dependency on cht-conf. 
INFO Checking that https://medic: **** @clone-smarthealth-ke-prod-3-13.lg-apps.com/medic/ is available... 
INFO Processing config in config-lg-forked. 
INFO Actions:
     - edit-contacts 
INFO Starting action: edit-contacts… 
WARN No csv directory found at /Users/jackiemoraa/Documents/Work/config-lg-forked/csv. 
INFO edit-contacts complete. 

```

b) I have created another file and saved it as suggested.  
Running that then I get the error you anticipated

 ![Screenshot 2022-07-04 at 16.15.31](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/0/0cb0155abe743b69980321e3119d5b43ca2ba67a.png)

Thus, replacing the 123 with my uuid

```auto
INFO Checking that https://medic: **** @clone-smarthealth-ke-prod-3-13.lg-apps.com/medic/ is available... 
INFO Processing config in config-lg-kenya. 
INFO Actions:
     - edit-contacts 
INFO Starting action: edit-contacts… 
INFO Processing CSV file: /Users/jackiemoraa/Documents/Work/config-lg-forked/config-lg-kenya/csv/contact.csv … 
INFO Downloading doc(s)... 
WARN No columns specified, the script will add all the columns in the CSV! 
INFO edit-contacts complete. 

```

I can see a json file added in the json\_docs folder containing all the fields for the user with the specified uuid. So this route works 🙂

What I am looking to achieve: given the a user’s uuid, I want to add a new flag to the docs to be able to restrict forms. In this way, if I have e.g. 100 UUID, it will download all the 100 user couch docs then I’ll need to edit all of them to add the flag then upload them?  
I have been able to successfully do it for the one user using the `upload-docs` command, but I am just wondering about the scenario where I am anticipating hundreds…

 ![Screenshot 2022-07-04 at 16.56.11](https://communityhealthtoolkit.b-cdn.net/uploads/default/original/2X/3/35808388add02881f4d1845ba9de5b9106dad0f8.png)

---

<div class="post-metadata">

**Author:** ![gareth](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/gareth/32/62_2.png) [@gareth](https://forum.communityhealthtoolkit.org/u/gareth)\
**Post date:** [July 4, 2022, 6:30pm UTC](https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968/8 "2022-07-04T18:30:10Z")

</div>

> [@Kymoraa](#):
>
> I want to add a new flag to the docs to be able to restrict forms. In this way, if I have e.g. 100 UUID, it will download all the 100 user couch docs then I’ll need to edit all of them to add the flag then upload them?

Given that the simple version works I think there’s something wrong with the original CSV which means it’s not parsing properly. To automatically add your column create a CSV like…

```auto
documentID, is_in_fp_pilot
123, true
456, true

```

Then when you run edit-contacts you should see two JSON files with the new property added. Verify that these are correct, then you can use upload-docs to upload them to the server.

Note that the upload-docs command cannot be undone so be sure to test it on a test server before running against production.

---

<div class="post-metadata">

**Author:** ![Kymoraa](https://communityhealthtoolkit.b-cdn.net/user_avatar/forum.communityhealthtoolkit.org/kymoraa/32/942_2.png) [@Kymoraa](https://forum.communityhealthtoolkit.org/u/Kymoraa)\
**Post date:** [July 5, 2022, 8:28am UTC](https://forum.communityhealthtoolkit.org/t/errors-editing-updating-existing-documents-in-couchdb/1968/9 "2022-07-05T08:28:05Z")

</div>

> [@gareth](#):
>
> Given that the simple version works I think there’s something wrong with the original CSV which means it’s not parsing properly. To automatically add your column create a CSV like…
> 
> ```auto
> documentID, is_in_fp_pilot
> 123, true
> 456, true
> 
> ```

I have implemented it with the flag while downloading and later uploading the docs, and now I can confirm that the flags have been updated correctly for 3 test uuids. Thank you for this @gareth  
I am still having challenges with having it as a CSV file and I am just editing it as a text file, which also runs some errors at times. But I think now I can just work around this.

```auto
ERROR Invalid Record Length: expect 2, got 3 on line 4 

```

> [@gareth](#):
>
> Note that the upload-docs command cannot be undone so be sure to test it on a test server before running against production.

Sure, we’ll run it against a dev instance and confirm before going live with it.  
Thank you very much!
