2

I make a lowdb request to update a JSON file, but only the date is updated. When I make a console.log of the information to update, this is what I want to update:

res.on("end", function () {
            let body = Buffer.concat(chunks);
            let final = JSON.parse(body);
            if (final.error !== undefined) {
                console.log("Initial authentication:", final.error_description, "Please refresh the authentication grant");
                extAuthCallback(84);
            } else {
                tokens.set('access_token', final.access_token)
                    .set('expires_in', final.expires_in)
                    .set('refresh_token', final.refresh_token)
                    .set('refresh_date', moment())
                    .write()
                console.log(final, tokens.value())
                extAuthCallback(1);
            }
        });

console.log of my final variable:

{
  access_token: 'oa_prod_iq00cRPk5Jhh4VffSHlDj7DEDsSIlpCRRczI3l3ASC0',
  token_type: 'bearer',
  expires_in: 2399,
  refresh_token: 'oa_prod_nIjBZs74xGvJXi1B-wdMyITfxGyklpCRRczI3l3ASC0'
}

console.log of my JSON file after the request:

{
  access_token: 'oa_prod_pB9Q0FFM9Tk4c5n3HMRBFKAVz6naiJ-jmb3QCeBrT00',
  expires_in: 2399,
  refresh_token: 'oa_prod_nX3EDs530SM8eHv_fM5BN7-5RLBwkrKoUi6uExBbTY4',
  refresh_date: '2020-11-28T23:31:13.855Z',
  primary_autorization_date: '2020-11-29T00:40:58.421Z'
}

My JSON file after the modifications:

{
  "access_token": "oa_prod_pB9Q0FFM9Tk4c5n3HMRBFKAVz6naiJ-jmb3QCeBrT00",
  "expires_in": 2399,
  "refresh_token": "oa_prod_nX3EDs530SM8eHv_fM5BN7-5RLBwkrKoUi6uExBbTY4",
  "refresh_date": "2020-11-28T23:31:13.855Z",
  "primary_autorization_date": "2020-11-29T00:40:58.421Z"
}

So it only has the primary_autorization_date field changing...

dolor3sh4ze
  • 925
  • 1
  • 7
  • 25

2 Answers2

2

You should use set instead of update.

tokens.set('access_token', final.access_token)
    .set('expires_in', final.expires_in)
    .set('refresh_token', final.refresh_token)
    .set('refresh_date', moment())
    .write()

The update method is accepted a function like this.

db.update('test1', (n) => 5555)
  .update('test2', (n) => n + 1)
  .write()

If you use set, you just need to assign the value to it.

db.set('test1', 5555).set('test2', 3333).write()

And when you use moment, there are two ways to you could use.

// Way 1 with moment()
db.set('date', moment()).write()

// Way 2 with moment
db.update('date', moment).write()
Jack Yu
  • 2,190
  • 1
  • 8
  • 14
  • 1
    So yes at the beginning I was using `.set` but it didn't change anything in my JSON file. When I made a console.log of `tokens.value()` I realized that it was modified but the modification doesn't appear in my JSON file... – dolor3sh4ze Nov 29 '20 at 13:37
  • 1
    Where do you write this filed `primary_autorization_date`? I don't see any code to `set` or `update` this field. – Jack Yu Nov 29 '20 at 14:21
  • I've found the solution, I redact this within 1 hour. Thank's for your help! – dolor3sh4ze Nov 29 '20 at 14:22
0

So the solution is:

I call my function that contains the HTTP request in another file like this:

app.get('/', async function(req, res) {
    const access_token = req.query.code;
    if (access_token) {
        let authentication = await asyncExtAuth(access_token);
        if (authentication == 84)
            return res.send({error: "Please give a valid access_token"});
        res.sendFile(path.join(__dirname + '/success_page/index.html'));
        db.set('access_token', access_token).write()
        tokens.set('primary_autorization_date', moment()).write()
        console.log("Access Token successfully refreshed")
    }
    else
        res.send({error: "Please specify an access token"})
})

In which I modify a second time my file with the line tokens.set('primary_autorization_date', moment()).write(). By doing this, lowdb doesn't take into account the modification made just before and re-modifies my file with the information it contained before. The solution is to add the line tokens.read() just before modifying the file to update the cache:

app.get('/', async function(req, res) {
    const access_token = req.query.code;
    if (access_token) {
        let authentication = await asyncExtAuth(access_token);
        if (authentication == 84)
            return res.send({error: "Please give a valid access_token"});
        res.sendFile(path.join(__dirname + '/success_page/index.html'));
        db.set('access_token', access_token).write()
        tokens.read()
        tokens.set('primary_autorization_date', moment()).write()
        console.log("Access Token successfully refreshed")
    }
    else
        res.send({error: "Please specify an access token"})
})
dolor3sh4ze
  • 925
  • 1
  • 7
  • 25