How to use “await” in sails when creating a new record

前端 未结 2 1797
醉酒成梦
醉酒成梦 2021-01-14 12:17

I want to use \"await\"

According to the sails documentation I act as follows:
https://sailsjs.com/documentation/reference/waterline-orm/models/create

<
2条回答
  •  感情败类
    2021-01-14 12:48

    SyntaxError: await is only valid in async function

    This is because you are using await in a function that is not async

    Remember, the await keyword is only valid inside async functions. If you use it outside of an async function's body, you will get a SyntaxError.

    Source MDN async function

    You need to make the function async for it to work. Making those changes in your code,

    'use strict';
    
    create: async function(req, res, next) {
            var new_place = await Place.create({ ... }, function place_created(err, XX) {
                if (err && err.invalidAttributes) {
                    return res.json({ 'status': false, 'errors': err.Errors });
                }
            }).fetch();
            if (new_place) {
                console.log(new_place);
                res.json({ 'status': true, 'result': new_place });
            }
        },
    

提交回复
热议问题