【问题标题】:How can I delete the results from a 'where' query in Azure Mobile Services?如何从 Azure 移动服务中的“位置”查询中删除结果?
【发布时间】:2018-07-03 17:37:46
【问题描述】:

我让 Azure 运行带有 Azure-mobile-apps 包 "azure-mobile-apps": "^3.0.1" 的 Node 服务器。我可以使用req.azureMobile 从 EasyAPI 文件中的任何给定表中获取结果:

module.exports = {
    get: asyncMiddleware(async (req, res, next) => {
        const deviceTable = req.azureMobile.tables('Devices');

        var results = await deviceTable.where(function(date) {
            return this.last_active < date
        }, new Date(2018,5,5)).read();

        res.status(201).send(results);
    })
};

但是如果我想删除where 子句的结果呢?

module.exports = {
    delete: asyncMiddleware(async (req, res, next) => {
        const deviceTable = req.azureMobile.tables('Devices');

        await deviceTable.where(function(date) {
            return this.last_active < date
        }, new Date(2018,5,5)).delete();

        res.status(201).send();
    })
};

我愚蠢地尝试了上述方法,但没有奏效。我发现 azure-mobile-apps 的文档很难找到也很难理解。我找到了这个页面right here,但这并没有告诉我可以使用什么。

【问题讨论】:

    标签: node.js azure azure-mobile-services


    【解决方案1】:

    好的,我想我已经找到了答案,但并不完全确定。我试过这个,它似乎完全软删除了我试图删除的记录:

    module.exports = {
        delete: asyncMiddleware(async (req, res, next) => {
            const deviceTable = req.azureMobile.tables('Devices');
    
            await deviceTable.delete(deviceTable.where(function(date) {
                return this.last_active < date
            }, new Date(2018,5,5)));
    
            res.status(201).send();
        })
    };
    

    烦人的是,当有 0 条记录要删除时,这似乎是错误,这对我来说似乎是出乎意料的行为。也许我做错了什么......

    【讨论】:

      【解决方案2】:

      我遵循了您的代码并在我这边测试了以下代码:

      req.azureMobile.tables('TodoItem')
         .delete(req.azureMobile.tables('TodoItem')
         .where(function(id){return this.id ==id;},req.query.id))
         .then(results=>{
             //res.json(results);
              res.status(201).send(results);
         })
         .catch(next);
      

      在测试上述代码之前,我为我的 Web 应用启用了diagnostics logging,并在 Azure 门户上转到我的 Web 应用的“监控 > 日志流”以查看实时日志记录。

      发送删除请求后,我可以看到如下日志:

      2018-07-03T08:59:48.551Z - �[35msilly�[39m: 执行 SQL 语句 SELECT * FROM [dbo].[TodoItem] WHERE ([id] = @p1);DELETE FROM [ dbo].[TodoItem] WHERE ([id] = @p1);SELECT @@rowcount AS recordsAffected;带参数 [{"name":"p1","pos":1,"value":"123"}]

      为了一个简单的方法,我只是去我的应用服务的“MOBILE > Easy tables”,发现我之前删除的记录已经被完全删除了。另外,我尝试通过添加一个额外的查询参数__includeDeleted 来通过浏览器检索之前的记录,如下所示:

      https://{your-mobile-app}.azurewebsites.net/tables/todoitem/{record-id}?ZUMO-API-VERSION=2.0.0&__includeDeleted=true
      

      注意:如果记录不存在,我会收到404状态码和以下短信:

      {
          "error": "The item does not exist"
      }
      

      根据我的测试,上述流程也可以实现如下:

       req.azureMobile.tables('TodoItem')
          .delete({id:req.query.id})
          .then(results=>{
             //res.json(results);
              res.status(201).send(results);
         });
      

      根据我的测试,上述删除操作会完全删除记录,而不是将deleted字段设置为true,如果记录不退出,则会收到404错误。

      此外,我检查了即使记录不存在,我们也可以使用execute(statement) 毫无例外地执行自定义SQL 语句。这里是sn-p的代码,大家可以参考一下:

      var query = {
          sql: 'UPDATE TodoItem SET DELETED=1 where id=@id',
          parameters: [
              {name:'id',value:req.query.id}
          ]
      };
      
      req.azureMobile.data.execute(query).then(function (results) {
          res.json(results);
      });
      

      【讨论】:

      • 在您说From my testing, the above process could also be implemented as follows: 之后的部分中,该代码仅在我有 ID 时适用,而不是在我的用例中,这是一个更复杂的查询(我故意将我的示例作为日期包含在内)比较,因为这是我的需要)。出于某种原因,当我使用var result = await delete(where()) 时,delete 的承诺实际上并没有返回任何东西。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-04
      • 2022-11-29
      • 2021-04-01
      相关资源
      最近更新 更多