2 回答

TA貢獻1826條經驗 獲得超6個贊
您應該使用set而不是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()
更新方法接受這樣的函數(shù)。
db.update('test1', (n) => 5555)
.update('test2', (n) => n + 1)
.write()
如果使用set,則只需為其賦值即可。
db.set('test1', 5555).set('test2', 3333).write()
當您使用 時moment,有兩種方法可以使用。
// Way 1 with moment()
db.set('date', moment()).write()
// Way 2 with moment
db.update('date', moment).write()

TA貢獻2011條經驗 獲得超2個贊
所以解決辦法是:
我在另一個文件中調用包含 HTTP 請求的函數(shù),如下所示:
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"})
})
其中我使用行第二次修改我的文件tokens.set('primary_autorization_date', moment()).write()。通過這樣做,lowdb 不會考慮之前所做的修改,而是使用之前包含的信息重新修改我的文件。解決方案是在修改文件之前添加以下行tokens.read()以更新緩存:
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"})
})
添加回答
舉報