Cannot get my database working

I deployed my basic tic-tac-toe app on glitch which uses nedb to store leaderboard. After reading I put the database file in .data folder in my app directory but wherever the the app works it does not write in that database file. The database file remains empty and after ever server restart the leaderboard seems to reset. Any help would be appriciated.

My server side code:
const express = require(‘express’)
const dataStore = require(‘nedb’)

const app = express()

const dataBase = new dataStore({filename: '.data/database', autoload: true })
dataBase.loadDatabase()

app.listen(process.env.PORT, () => {
	console.log('Listening')
})

app.use(express.static('public'))
app.use(express.json({ limit: '100kb' }))

app.post('/name', (request, response) => {
	const data = request.body
	console.log(data)
	dataBase.find({ name: data.name }, (err, docs) => {
		if (docs.length) {
			dataBase.update(
				{ name: data.name },
				{ name: data.name, matches: data.matches }
			)
			dataBase.loadDatabase()
			console.log('Updated!')
		} else {
			dataBase.insert(data)
			console.log('Inserted!')
		}
	})
	response.end()
	//response.json({ status: "Success" });
})

app.get('/name', (request, response) => {
	dataBase
		.find({})
		.sort({ matches: -1 })
		.exec((err, data) => {
			if (err) {
				response.end()
				return
			}
			response.json(data)
		})
})

Run refresh in the console or refresh the page. Edits made by the console will not auto update. Also, .data does not show.

3 Likes

If you would like to see what is in the .data folder, run, ls .data.

1 Like

The edits are not made by the console, they are made by the server. I am just logging in the console. I created the .data folder so I know what is in it.

1 Like

Same difference.

This is the only way @Hugher256!

Go to the Terminal of your glitch project and run this:

refresh
cat .data/database

Hit enter when the refresh is done. You shouldn’t see the .data folder, which is good. Now the last command is ran. Now, your database is shown!

You can also temporarily rename the folder to a different name and only run refresh. Make sure to change it later, as you wouldn’t want a bunch of names copied.

1 Like

And @chessebuilderman the ls command only shows the files of a folder, not the content of a single file

I know that.

Mkay, sorry for reading your post wrong ):

Thanks for the help guys :slight_smile:

1 Like