当我尝试创建新用户时出现意外令牌用户 = 等待 User.create(用户)

Unexpected token user = await User.create(user) when i try to create a new user

我正在尝试使用 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)
    }
}

函数“userLogged()” return 我在授权中收到令牌的用户 header。

所以我尝试:

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;

但我收到了:

Unexpected token user = await User.create(user)

您正在尝试 await 不在 async 函数中的东西,特别是您的承诺解析器 .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
    })
}

此外,根据提供的代码,store 不需要异步,因为您没有 await 在其范围的顶层处理任何内容。