我正在尝试使用最新的mongoose doc中所述的insertMany:
test.utility.js
import mongoose from 'mongoose';
import Groups from '../models/group.model';
const utils = {
createMultipleGroups(n) {
if (n > 26) {
console.log('Cannot create more than 26 groups');
return false;
}
const letters = 'ABCDEFGHJIKLMNOPQRSTUVWXYZ';
const multiGroups = [];
for (let i = 0; i < n; i += 1) {
multiGroups.push({ name: `Group${letters.charAt(i)}`, description: `Description Group${letters.charAt(i)}` });
}
console.log('UTILITY multiGroups array: %j', multiGroups);
// UTILITY multiGroups array
// [{"name":"GroupA","description":"Description GroupA"}
//. {"name":"GroupB","description":"Description GroupB"},
// {"name":"GroupC","description":"Description GroupC"}]
console.log(mongoose.connection.readyState); // 2 connecting !
Groups.insertMany(multiGroups)
.then((savedGroups) => {
console.log('saved multi groups: ', savedGroups.length);
return savedGroups;
})
.catch((e) => {
console.log('error saving multi groups %j', e);
return [];
});
},
这个函数是从前钩子中的mocha测试中调用的
user.test.js
import mongoose from 'mongoose';
import request from 'supertest';
import httpStatus from 'http-status';
import chai, { expect } from 'chai';
import app from '../../../index';
import User from '../../models/user.model';
import utils from '../../helpers/tests.utility';
/**
* root level hooks
*/
before(() => {
const createdGroups = utils.createMultipleGroups(3);
console.log('created groups: %j', createdGroups); // undefined
});
但是它没有返回任何内容..并且在控制台中没有任何东西......来自test.utility控制台日志
在user.test控制台日志中返回时出现'undefined'
我哪里错了?
感谢您的反馈