简体   繁体   中英

Node.js then is not a function

I'm working on an open source project where we want to use a local json file as a database. We decided to use this package to handle the getters and setters ( https://github.com/typicode/lowdb ).

I've copied their example file that uses express, but I get the following error: db.defaults(...).write(...).then is not a function

This is my Server.js file:

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}`);
    });
});

I made sure to use stage-0 with babel, hoping it would fix this. But not avail. Any help would be great!

According to the documentation .write() only returns a promise if you use an async storage method. You have to do more than just require fileAsync to get that to work ;)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM