Create Mongoose collection on Express

橙三吉。 提交于 2020-01-06 06:58:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!