我正在运行API的早期测试并且错误应该是显而易见的,但是我会在45分钟内看到相同的代码。
这是错误
Creating User document find or create a twitter user:
TypeError: Cannot read property 'then' of undefined
这是测试。错误在于 .then(user => {...}
行const assert = require('assert');
const User = require('../models/userModel');
const userController = require('../controllers/usersController');
describe('Creating User document', () => {
it('find or create a twitter user', (done) => {
userController.findOrCreateHelper({
username : 'testuser',
displayName : 'testUserDisplay',
id : 111000100,
provider : 'Twitter'
})
.then(user => {
console.log('*** Error in this .then bracket ***', user)
assert(user === 'testuser')
assert(user.id === 111000100)
done()
}).catch(error => console.error(error))
})
这是控制器
const mongoose = require('mongoose');
const User = require('../models/userModel');
exports.findUser = ({id}) => {
return User.findOne({id})
}
exports.createUser = ({username, displayName, id, provider}) => {
return User.create( {username, displayName, id, provider })
}
exports.findOrCreateHelper = (provider) => {
exports.findUser({id})
.then(user => {
if ( !user ) {
exports.createUser(provider)
}
})
}
答案 0 :(得分:0)
exports.findOrCreateHelper需要"返回exports.findUser({...}} ..."如下所示:
exports.findOrCreateHelper = (provider) => {
return exports.findUser({id})
.then(user => {
if ( !user ) {
exports.createUser(provider)
}
})
}