【问题标题】:Unable to return user from service model无法从服务模型返回用户
【发布时间】:2019-06-16 16:51:55
【问题描述】:

我需要将用户从我的服务模型返回到控制器模型,但我无法做到这一点,有人可以帮我吗?

async function create(userParam) {


if (await User.findOne({ username: userParam.username })) {
    throw 'Username "' + userParam.username + '" is already taken';
}

const user = new User(userParam);
console.log("User", userParam , user )
// hash password
if (userParam.password) {
    user.hash = bcrypt.hashSync(userParam.password, 10);
}

// return user from here
await user.save();
} 

【问题讨论】:

  • return await user.save()
  • @Baruch awaitnot neccessaryreturn user.save() 应该够了

标签: javascript reactjs mongodb mongoose


【解决方案1】:

试试这个`

user.save().then(item =>{
 return item ;
}

`

【讨论】:

  • 这行不通。您将用户返回到承诺回调,而不是 create 函数。
猜你喜欢
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
  • 2014-05-12
  • 1970-01-01
  • 1970-01-01
  • 2021-07-12
  • 2019-07-12
  • 1970-01-01
相关资源
最近更新 更多