Azure移动服务-在插入脚本上更改用户模型

Azure Mobile Services - Alter User model on Insert script

本文关键字:用户 模型 脚本 插入 移动 服务 Azure      更新时间:2023-09-26

我有一个reservation模型和一个user模型。在reservation模型的插入脚本中,我有以下内容:

function insert(item, user, request) {
    response.send(200, 'test');
    item.organizationid = user.organizationid;
    if (user.hourstoreserve <= (item.endtime - item.starttime)) {
        request.respond(400, 'You do not have the necessary hours available to make this reservation');
    } else if (user.complaints >= user.complaintsallowed) {
        request.respond(400, 'You are over your maximum number of allowed complaints this month.');        
    } else {
        user.hourstoreserve = (user.hourstoreserve - (item.endtime - item.starttime));
        request.execute();   
    };           
};

我需要确保item,这应该是我插入的新reservation,从我的user中获得organizationid。我还想确保user有它的hourstoreserve验证,如果reservationuserhourstoreserve应该降低。

看起来这个脚本根本没有被执行。第一个response.send(200, 'test');不发送响应。

我从我的自定义api中调用这个插入脚本,如下所示:

var reservations = request.service.tables.getTable("reservations");
reservations.insert(newReservation);

自定义API调用工作并插入预定,它似乎没有执行我的插入脚本。

当您从服务本身调用表的CRUD操作时(即,在自定义API、调度器或另一个表的代码中),不会执行表脚本。有一个开放的功能请求将此功能添加到后端,如果您认为它会对您有所帮助,请投票。

我在你的代码中看到的另一个问题-传递给插入脚本的user对象不是来自你的用户模型的项目;相反,它是关于登录到服务的用户的信息(即,在客户端,在调用一个登录操作之后,它将拥有的用户信息)。该对象没有任何关于组织id、hourstoreserve等的属性。如果需要获取这些信息,则需要查询用户表并直接使用它。