简体   繁体   English

在Sails API中调用之前更改数据

[英]Changing data before Create in Sails API call

in the TaskController I'm trying to insert the current user (ie the user who created the task) into the newly created task. TaskController我试图将当前用户(即创建任务的用户)插入到新创建的任务中。

Mostly I've been trying different ways to override create() but in the override call the original blueprint and let it continue as usual. 通常,我一直在尝试不同的方法来覆盖create()但是在覆盖中调用原始蓝图,并使其像往常一样继续进行。

I found a bunch of answers but all seem outdated or otherwise not working. 我找到了很多答案,但所有答案似乎都已过时或无法正常工作。 I wonder if there's something more updated. 我想知道是否还有更多更新。

I'm using Sails v1.0.0-37 我正在使用Sails v1.0.0-37

Solution: 解:

in \\tasks\\api\\controllers\\TaskController.js \\tasks\\api\\controllers\\TaskController.js

  create: function (req, res) {
    req.query.createdBy = req.user.id;
    Task.create(req.query)
      .exec(function (err) {
        if (err) {
          return res.serverError(err);
        }
        return res.ok();
      });
  }

My issue: 我的问题:

During all my testing and experimenting I created an empty lifecycle callback of beforeCreate. 在所有测试和试验过程中,我创建了一个空的beforeCreate 生命周期回调 Since it was empty, I never called cb() so the request just got stuck. 由于它是空的,所以我从未调用过cb()因此该请求被卡住了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM