在Express上创建Mongoose集合 [英] Create Mongoose collection on Express

查看:77
本文介绍了在Express上创建Mongoose集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在app.js上,我有这个:

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:

/config/database.js:

module.exports = {
  database: 'mongodb://localhost/nodekb',
  secret: ''
}

执行app.js的最后一部分,我在其中创建gameStatm,但是当我在cmd上键入"show collections"时,它不会显示此集合(尽管它会显示其他集合).我想念什么?

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?

推荐答案

我认为您需要调用newStat.save(function(err){...})来保存数据. 这是另一个例子我发现...

I think you need to call newStat.save(function(err){ ... }) to save the data. Here's another example I found ...

这篇关于在Express上创建Mongoose集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆