Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 如何创建用户sails 1.0操作2?_Node.js_Sails.js - Fatal编程技术网

Node.js 如何创建用户sails 1.0操作2?

Node.js 如何创建用户sails 1.0操作2?,node.js,sails.js,Node.js,Sails.js,我正在尝试用新的标准来创建一些东西,所以sails,有人能给我解释一下吗,你想了解更多关于使用action2的sails的信息吗,他们能替换控制器吗?在这种情况下,如何保存一个简单的用户 错误: 我给你举个例子,我们可以从那里开始工作 // api/controllers/user/create.js module.exports = { // Necessary Documentation friendlyName: 'User creation', descript

我正在尝试用新的标准来创建一些东西,所以sails,有人能给我解释一下吗,你想了解更多关于使用action2的sails的信息吗,他们能替换控制器吗?在这种情况下,如何保存一个简单的用户

错误:


我给你举个例子,我们可以从那里开始工作

// api/controllers/user/create.js
module.exports = {
    // Necessary Documentation
    friendlyName: 'User creation',

    description: 'Creates an user',
    // This will validate that every argument needed is received
    // from req.body, req.params or req.query (when comparing to a Express controller)
    // It also validates presence if `required` is true and correct typing
    inputs: {
        password: {
            description: 'Password',
            type: 'string',
            required: true
        },
        password_confirmation: {
            description: 'Password confirmation',
            type: 'string',
            required: true
        },
        email: {
            description: 'The email of the user to create',
            type: 'string',
            required: true
        },
    },
    // Since an action2 will be parsed to a Node machine, we need to specify an output
    // These will work very similar to callbacks with no error parameter
    exits: {
        // This is the name of a function we will have to use later
        created: {
            description: 'Created',
            // But this, is the name of the response Sails will invoke
            // You'll have to provide your own custom response:
            // https://sailsjs.com/documentation/concepts/extending-sails/custom-responses
            // I made something silly down there.
            responseType: 'created'
        },
        badRequest: {
            description: 'Invalid data',
            //This one, as well as ´ok´ are always available
            responseType: 'badRequest'
        },
    },
  // This is your procedure, you can see this as the code you'd put in your controller
    //  `this.req` will have your req object, but you cannot access `res`
    fn: async function create(inputs, exits){
        // We can validate that the passwords match
    if (inputs.password !== inputs.password_confirmation){
            // In case of failure, we answer with a badRequest response
      return exits.badRequest({errors: ['Password mismatch']});
    }
        //This is an async function, so we can use await and treat Promises as sync
    try{
            // We create here our user. 'inputs' is an object that contains all parameters
            // described before. Namely:  password, password_confirmation and email
            //I'm just using a placeholder with Sequelize.js syntax.
      const user = await User.create(inputs);
            // Finally if all went well, we can answer the request with an ok response
      return exits.created(user);
    }catch(error){
            // In case of error, it must be a validation from DB.
            // Maybe not, but it's- an example, so let's say it is.
            // I highly recommend you to make a better error handler than this.
      return exits.badRequest(error);
    }
  }
};


// api/responses/created.js
module.exports = function created(optionalData = null) {
  const statusCodeToSet = 201;

  return this.res.status(statusCodeToSet).send(optionalData);
};

// config/routes.js
module.exports= {
    "POST /user": "UserController.create",
}

// Finally, you can POST a JSON to /user
{
    "email": "this_is_a_test@me.com",
    "password": "12345678",
    "password_confirmation": "12345678"
}

// Also, if we had in our routes something like this:
module.exports= {
    "POST api/v:version/user": "UserController.create",
}

// We could add in our inputs object in the action this:
version: {
    type: 'number',
    required: true
}
// And we would have our version number in
// `inputs.version` inside our `fn` action

也许你可以发布你的代码,这样我们就可以告诉你出了什么问题。我建议你转到
adonis
framework:?帆有点过时了,不够灵活。