【问题标题】:How to Access tables from scripts in Azure Javascript App Services如何从 Azure Javascript 应用服务中的脚本访问表
【发布时间】:2016-01-08 18:43:56
【问题描述】:

我正在使用带有 javascript 后端的 azure 移动服务,使用 rest API。 Azure 告诉我从移动服务迁移到最新的应用服务。我从官方 azure 博客中阅读了这些更新,这些更新说明了 rest api 功能的一些重大变化:

https://azure.microsoft.com/en-us/blog/azure-mobile-apps-november-2015-update/

问题是我在新文档中没有找到如何从 Javascript App Services 中的脚本访问表。

在移动服务中,如果您想访问表的 crud,请执行以下操作:

exports.post = function(request, response) {
// Use "request.service" to access features of your mobile service, e.g.:
//   var tables = request.service.tables;

//accesing sample table
     var sampleTable = request.service.tables.getTable('sample');

// then you can do an insert, update, delete and select through sampleTable ...
...    
}

在应用服务中你有类似的东西:

module.exports = {
"post": function (req, res, next) {
 //there is no req.service.tables.getTable('TABLE_NAME');
    ...
}

所以我的问题是应用服务中的方法 tables.getTable() 在哪里以及如何访问它。官方文档中还有Mobile Services的对象定义,可以通过request.service.tables.getTable('TABLE_NAME')获取。

https://msdn.microsoft.com/en-us/library/azure/jj554218.aspx

微软还没有更新它的文档吗?

【问题讨论】:

    标签: javascript azure backend


    【解决方案1】:

    随着 Node.js 移动应用程序 SDK 向 GA 迈进,大量的文档工作正在发生,但我建议您从这里的 GitHub 项目页面https://github.com/Azure/azure-mobile-apps-node 开始,从那里开始,您不仅可以访问代码,而且指向有关如何执行您询问的任务的详细文档的链接。

    要记住的一点是,推荐的迁移升级是两件不同的事情。您可以将您的移动服务应用程序迁移到应用服务环境,而无需升级到移动应用程序 Node.js SDK。

    【讨论】:

    • 感谢您阐明迁移和升级之间的区别!事实上,迁移的移动服务会保留相同版本的 Node.js SDK。升级到较新的 Node.js SDK 后,您可以访问 Visual Studio Online 中输出视图的最新功能。
    【解决方案2】:

    供将来参考:更新的文档位于Mobile App API Doc

    要访问表结构和 CRUD 和自定义 API 背后的代码,您必须转到“Easy Tables and Easy APIs”选项。

    如何查看/编辑代码?

    1. 在 Azure 门户的移动应用程序中转到“Easy Tables”
    2. 如果您没有任何表,请创建一个并选择它,或者选择任何现有的
    3. 单击 Visual Studio Online 选项“编辑脚本”,您将看到代码整个服务代码结构和定义,如果您的表名为 Example,您将看到一个 Example.js,其中所有启动代码都已准备好 CRUD李>

    如何获取表格参考?

    // 获取当前表

    var table = azureMobileApps.table();

    //获取其他表引用

    var othertable = azureMobileApps.tables.table("othertable");

    【讨论】:

    • 您之前错过了 req 对象,而不是 azureMobileApps 它是 azureMobile 对象:var userTable = req.azureMobile.tables('user'),
    【解决方案3】:

    根据microsoft forums 中的回答,Microsoft 的人给出了在应用服务中执行此操作的正确方法如下:

    var queries = require('azure-mobile-apps/src/query'),
    bodyParser = require('body-parser');
    
    
    function replyWithError(response) {
        return function(err) {
            response.status(500).json({ error: err });
        }
    }
    
    
    module.exports = {
    "post": function (req, res, next) {
    
     //this is how we access the table   
     var userTable = req.azureMobile.tables('user'),
         query = queries.create('user');
    
       userTable.read(query).then(function(results) {
         res.status(200).json({ resultados: results });
     }).catch(replyWithError(res));
    
    }
    
    };
    

    我已经测试过了,效果很好!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-12-14
      • 2016-08-23
      • 2014-07-07
      • 2021-03-09
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多