# How to read values from db in provision script

**URL:** <https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300>\
**Category:** Uncategorized\
**Created:** [January 4, 2022, 7:34am UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300 "2022-01-04T07:34:46Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![techgenie](https://avatars.discourse-cdn.com/v4/letter/t/8c91f0/32.png) [@techgenie](https://forum.genieacs.com/u/techgenie)\
**Post date:** [January 4, 2022, 7:34am UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/1 "2022-01-04T07:34:46Z")

</div>

I need to trigger firmware upgrade to CPE based on the software version that the CPE reports. Download URL is present in db and would like to read the name, url, filesize, version etc from db in provision script, compare the version and trigger the firmware update to CPE accordingly .

Please let me know how to do this.

---

<div class="post-metadata">

**Author:** ![Ars2pik](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Ars2pik](https://forum.genieacs.com/u/Ars2pik)\
**Post date:** [October 3, 2023, 9:33am UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/2 "2023-10-03T09:33:40Z")

</div>

Good morning,  
I have the same problem.  
Did you solve it?

---

<div class="post-metadata">

**Author:** ![Felipe](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/felipe/32/1949_2.png) [@Felipe](https://forum.genieacs.com/u/Felipe)\
**Post date:** [October 3, 2023, 11:46am UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/3 "2023-10-03T11:46:06Z")

</div>

You can do it with an ext script, using moongose for example.

---

<div class="post-metadata">

**Author:** ![Ars2pik](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Ars2pik](https://forum.genieacs.com/u/Ars2pik)\
**Post date:** [October 3, 2023, 1:59pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/4 "2023-10-03T13:59:19Z")

</div>

do you have a concrete example to do this?  
Thanks for your help

---

<div class="post-metadata">

**Author:** ![akcoder](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/akcoder/32/11_2.png) [@akcoder](https://forum.genieacs.com/u/akcoder)\
**Post date:** [October 3, 2023, 5:34pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/5 "2023-10-03T17:34:29Z")

</div>

> **[Example of a Provisioning Flow](https://github.com/genieacs/genieacs/wiki/Example-of-a-Provisioning-Flow)**
>
> A fast and lightweight TR-069 Auto Configuration Server (ACS) - genieacs/genieacs

---

<div class="post-metadata">

**Author:** ![Felipe](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/felipe/32/1949_2.png) [@Felipe](https://forum.genieacs.com/u/Felipe)\
**Post date:** [October 3, 2023, 5:35pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/6 "2023-10-03T17:35:21Z")

</div>

Yes, for sure.

First you’ll need create a ext directory in /opt/genieacs, I think you already did it in installation. After in you provision script you can call you external script, this way:

```auto
const scriptReturn = ext('filename', 'fileFunction', param1, param2)

```

In you script you can do whatever you want, like find something at mongo and return, for exemple.

Exemple of an ext script:

```auto
const fetch = require('node-fetch')

function configFile(args, callback) {
  let params = JSON.parse(args[0])
  let req = fetch(
    `http://localhost:3333/verification/device-id/${params.deviceId}`,
  ).then((req) => {
    let res = req.json().then((res) => {
      return callback(null, JSON.stringify(res))
    })
  })
}

exports.configFile = configFile

```

This script fetch to url that’s my API, but you can have your all code here.

---

<div class="post-metadata">

**Author:** ![Ars2pik](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Ars2pik](https://forum.genieacs.com/u/Ars2pik)\
**Post date:** [October 4, 2023, 8:02am UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/7 "2023-10-04T08:02:58Z")

</div>

Provision script :

> let files = ext(‘file-fetch.js’, ‘file’);//, JSON.stringify(args));  
> log(files);  
> if (!files) {  
> log(‘No files returned from API’);  
> return;  
> }

Ext script :

> const fetch = require(‘node-fetch’)
> 
> function file () {
> 
> ```
> const response = fetch('http://192.168.1.12:7557/files');
> 
> ```
> 
> // const body = response.text();
> 
> // console.log(body);  
> console.log(response);  
> }  
> exports.file = file;

I think my script is correct but it gives me the error “Extension timed out”

---

<div class="post-metadata">

**Author:** ![Felipe](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/felipe/32/1949_2.png) [@Felipe](https://forum.genieacs.com/u/Felipe)\
**Post date:** [October 4, 2023, 11:53am UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/8 "2023-10-04T11:53:36Z")

</div>

Check:

1. If the name of file it’s right
2. did you restart cmwp service after create the file
3. your directory its in the right path
4. Are you sure that `const response = fetch('http://192.168.1.12:7557/files');` returns something?

---

<div class="post-metadata">

**Author:** ![Felipe](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/felipe/32/1949_2.png) [@Felipe](https://forum.genieacs.com/u/Felipe)\
**Post date:** [October 4, 2023, 11:54am UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/9 "2023-10-04T11:54:29Z")

</div>

In your script you have no returns, just a `console.log()`

---

<div class="post-metadata">

**Author:** ![Ars2pik](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Ars2pik](https://forum.genieacs.com/u/Ars2pik)\
**Post date:** [October 4, 2023, 12:35pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/10 "2023-10-04T12:35:42Z")

</div>

I want to retrieve the different values ​​of filename below. :

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/genieacs/original/2X/4/493b82bb3f9699443af40ecc3c16d56c00bf35b3.png)

I checked points 1 to 3. OK  
For the point 4 you see result in my browser.

Provision script :

> let callback = “”  
> const files = ext(‘file-fetch.js’, ‘file’,JSON.stringify(callback) );  
> if (!files) {  
> log(‘No files returned from API’);  
> log(callback);  
> return;  
> }

Ext script :

> const fetch = require(‘node-fetch’);
> 
> async function file(callback) {  
> try {  
> const response = await fetch(‘[http://192.168.1.12:7557/files](http://192.168.1.12:7557/files)’);  
> const body = await response.text();  
> console.log(body);
> 
> ```
> return callback(body)
> 
> } catch (error) {
> console.error('Une erreur s\'est produite :', error);
> }
> 
> ```
> 
> }
> 
> exports.file = file;

I have add a command ‘return’ and i same error “Extension timed out”

---

<div class="post-metadata">

**Author:** ![Ars2pik](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Ars2pik](https://forum.genieacs.com/u/Ars2pik)\
**Post date:** [October 4, 2023, 12:58pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/11 "2023-10-04T12:58:51Z")

</div>

I did a test with " mongoose " and I got the same error.

Ext script :

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/genieacs/original/2X/e/ecee3605ecbc9394a021f9aeb29d621d154492b9.png)

---

<div class="post-metadata">

**Author:** ![Felipe](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/felipe/32/1949_2.png) [@Felipe](https://forum.genieacs.com/u/Felipe)\
**Post date:** [October 4, 2023, 2:07pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/12 "2023-10-04T14:07:17Z")

</div>

Could you please share your all provision script? What args do you give to ext script?

---

<div class="post-metadata">

**Author:** ![Felipe](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/felipe/32/1949_2.png) [@Felipe](https://forum.genieacs.com/u/Felipe)\
**Post date:** [October 4, 2023, 2:10pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/13 "2023-10-04T14:10:21Z")

</div>

It seems that you don’t need args, so, try this:

```auto
async function file(callback) {
    let req = fetch(
        `http://192.168.1.12:7557/files`,
    ).then((req) => {
        let res = req.json().then((res) => {
        return callback(null, JSON.stringify(res))
        })
    })
}
    
exports.file = file;

```

---

<div class="post-metadata">

**Author:** ![Ars2pik](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Ars2pik](https://forum.genieacs.com/u/Ars2pik)\
**Post date:** [October 4, 2023, 2:31pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/14 "2023-10-04T14:31:57Z")

</div>

> let callback = “”  
> const files = ext(‘file-fetch2.js’, ‘file’,callback);//,JSON.stringify(callback) );  
> if (!files) {  
> log(‘No files returned from API’);  
> log(callback);  
> return;  
> }

---

<div class="post-metadata">

**Author:** ![Ars2pik](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Ars2pik](https://forum.genieacs.com/u/Ars2pik)\
**Post date:** [October 4, 2023, 2:33pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/15 "2023-10-04T14:33:49Z")

</div>

Your script return this error :

name: TypeError  
message: callback is not a function  
stack: |-  
TypeError: callback is not a function  
at /opt/genieacs/ext/file-fetch2.js:9:16  
at process.processTicksAndRejections (node:internal/process/task\_queues:95:5)

---

<div class="post-metadata">

**Author:** ![Felipe](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/felipe/32/1949_2.png) [@Felipe](https://forum.genieacs.com/u/Felipe)\
**Post date:** [October 4, 2023, 2:41pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/16 "2023-10-04T14:41:51Z")

</div>

In your provision try:

```auto
const files = ext('file-fetch2', 'file');

if(!files) {
   log('No files returned from API')
}

```

You don’t need to give extension file (.js)

---

<div class="post-metadata">

**Author:** ![Ars2pik](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Ars2pik](https://forum.genieacs.com/u/Ars2pik)\
**Post date:** [October 4, 2023, 2:56pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/17 "2023-10-04T14:56:35Z")

</div>

This doesn’t work I still have the same error

---

<div class="post-metadata">

**Author:** ![akcoder](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.genieacs.com/akcoder/32/11_2.png) [@akcoder](https://forum.genieacs.com/u/akcoder)\
**Post date:** [October 4, 2023, 7:59pm UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/18 "2023-10-04T19:59:53Z")

</div>

I presume you are trying to automatically update the config/firmware on your devices? A [more resilient way is outlined in this post](https://forum.genieacs.com/t/massive-firmware-upgrade/797/2).

Also, every time you change an external script, you have to restart the CWMP process for the changes to be picked up.

---

<div class="post-metadata">

**Author:** ![Ars2pik](https://avatars.discourse-cdn.com/v4/letter/a/e495f1/32.png) [@Ars2pik](https://forum.genieacs.com/u/Ars2pik)\
**Post date:** [October 5, 2023, 7:59am UTC](https://forum.genieacs.com/t/how-to-read-values-from-db-in-provision-script/2300/19 "2023-10-05T07:59:22Z")

</div>

Hello yes . I want to retrieve the names of the configuration files added to genieacs so that I can then use the script to retrieve the most recent configuration file for each router by filtering the file names with the serial numbers of the routers. Concretely I want to be able to do versioning. THANKS
