问题
On app.js, I have this:
let Gamestat = require('./models/gamestat');
mongoose.connect(config.database, {
useMongoClient: true
});
let db = mongoose.connection;
io.on('connection', (socket) => {
(...)
socket.on('gameEnded', (data) => {
socket.broadcast.to(data.room).emit('gameEnd', data);
//add game stats to database
let newStat = new Gamestat({
time: 'testing',
winner: 'testing',
loser: 'testing',
moves: 'testing'
});
});
});
/config/database.js:
module.exports = {
database: 'mongodb://localhost/nodekb',
secret: ''
}
This last part of app.js, where I create gameStatm is executed, but when I type "show collections" on cmd, it doesn't show this collection (although it shows the others). What am I missing?
回答1:
I think you need to call newStat.save(function(err){ ... }) to save the data. Here's another example I found ...
来源:https://stackoverflow.com/questions/49765598/create-mongoose-collection-on-express