我正在尝试使用node.js / adonis创建一个新用户
我创建了这两个功能:
const User = use("App/Models/User")
async store ({ request, auth }){
let user = new User()
user = request.all()
this.userLogged(auth).then(res => {
user.user_id = res.id
console.log(user)
user = await User.create(user)
const token = await auth.generate(user)
Object.assign(user, token)
return user
})
}
async userLogged(auth) {
try {
return await auth.getUser()
} catch (error) {
console.log(error)
}
}
Run Code Online (Sandbox Code Playgroud)
函数“ userLogged()”返回我在授权标头中收到令牌的用户。
所以我尝试:
create a new instance of User;
put the request data in this instance;
take the user_id of the authorization header and put in the user.user_id;
create the user with the user_id;
take the token of the registred user;
put in the object user the token;
return the user;
Run Code Online (Sandbox Code Playgroud)
但我收到:
意外的令牌用户=等待User.create(用户)
小智 5
您正在尝试await执行async函数中未包含的内容,特别是您的promise resolver中的回调.then。
async store ({ request, auth }){
let user = new User()
user = request.all()
this.userLogged(auth).then(async res => {
user.user_id = res.id
console.log(user)
user = await User.create(user)
const token = await auth.generate(user)
Object.assign(user, token)
return user
})
}Run Code Online (Sandbox Code Playgroud)
另外,根据提供的代码,store不需要异步,因为您不需要await在其作用域的顶层进行任何操作。
| 归档时间: |
|
| 查看次数: |
59 次 |
| 最近记录: |