我正在一个开源项目中,我们想使用本地json文件作为数据库。我们决定使用此包来处理getter和setter(https://github.com/typicode/lowdb)。

我已经复制了使用express的示例文件,但是出现以下错误:db.defaults(...).write(...).then is not a function

这是我的Server.js文件:

import express from 'express'
import session from 'express-session'
import bodyParser from 'body-parser'
import promisify from 'es6-promisify'
import cors from 'cors'
import low from 'lowdb'
import fileAsync from 'lowdb/lib/storages/file-async'

import defaultdb from './models/Pages'

import routes from './routes/index.js'

const app = express();

const db = low('./core/db/index.json')

app.use(cors())

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

app.use('/', routes);

app.set('port', process.env.PORT || 1337);

db.defaults(defaultdb).write().then(() => {
    app.listen(app.get('port'), () => {
      console.log(`Express running → PORT ${server.address().port}`);
    });
});


我确保与babel一起使用Stage-0,希望它可以解决此问题。但无济于事。任何帮助将是巨大的!

最佳答案

According to the documentation .write()仅在使用异步存储方法时返回promise。您要做的不只是要求fileAsync来使它起作用;)

const db = low('./core/db/index.json', { storage: fileAsync })

关于javascript - 然后Node.js不是函数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44855680/

10-13 07:27