javascript - Mongoose 返回一个空数组

标签 javascript node.js mongodb mongoose

当我尝试使用 find() 函数时,Mongoose 返回一个空数组。有人可以帮我解决这个问题吗?

users.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;    
mongoose.Promise = global.Promise;

const userSchema = new Schema({
    name: String    
});
const user = mongoose.model('user', userSchema, 'users');    
module.exports = user;

test_helper.js

   const mongoose = require('mongoose');

    mongoose.connect('mongodb://localhost/user_test');

    before((done)=>{
        mongoose.connection.once('open',()=>{
            console.log('Connected');
        }).on('error', ()=>{
            console.log('Error');
        });
        done();
    });

    beforeEach((done)=>{
        mongoose.connection
                .collections
                .users
                .drop(()=>{
            done();
        });
     });

create_user.js

const assert = require('assert');
const User = require('../src/user');


describe('Create a user',()=>{
    it('it should create a new user',(done)=>{
        let joe = new User({
            name : "Joe"
        });
        joe.save().then(()=>{
            assert(!joe.isNew);
            done();
        });
    });
})

reading-users.js

const assert = require('assert');
const User = require('../src/user');


describe('This will read all users', () => {
    before((done) => {
        joe = new User({name:'joe'});
        joe.save().then(() => done());
    });
    it('It Should find all users named joe', (done) => {
        User.findOne({name:"Joe"})
            .then((foo)=>{
                //assert(users[0]._id.toString()===joe._id.toString());
                console.log(foo);
                console.log(joe);
                done();
        });
    });
});

=============================日志================= =========================

(node:3324) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect. Create a user Connected √ it should create a new user (274ms)

This will read all users [] { _id: 5bfa98eb6bd05d0cfc5d639d, name: 'joe', __v: 0 } √ It Should find all users named joe

2 passing (1s)

最佳答案

这个问题的根本原因是每次开始测试时,创建的记录总是会在测试开始之前被删除。这发生在 test_helper.js 文件的 beforeEach block 内。尝试用 afterEach 替换 beforeEach

afterEach((done)=>{
    mongoose.connection.collections.users.drop(()=>{
        done();
    });
});

关于javascript - Mongoose 返回一个空数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53467441/

相关文章:

javascript - 有没有办法更改 Node.js 网络(套接字)库中的 "on"监听器?

node.js - 使用 puppeteer 和 MutationObserver 检测 DOM 变化

javascript - grpc和express服务器可以由同一个nodejs服务器运行吗,或者grpc必须是不同的服务器

javascript - 如何使用 res.writeHead(301, { "Location": "/path/within/site" })?) 在 http node.js 的 res.writehead 中附加警报消息

node.js - MongoDB 更新/插入整个集合(数组)

javascript - (jquery) 如何在 [div] 元素中卡住鼠标光标(指针)?

javascript - 递归函数返回数组中的随机元素返回未定义

mongodb - MongoDB 中的 $elemMatch 查询

mongodb - MongoDB 可以为单个服务器生成多少个唯一 ID?

javascript - 如何在javascript中为全局变量赋值?