javascript - Azure表存储的插入功能中查询其他表

标签 javascript node.js azure azure-table-storage azure-mobile-services

我正在使用 Azure 表存储在插入新项目后发送推送通知。目前,我正在尝试查询第二个表以检索我想用于推送通知的字符串。

我对 Node.js 很陌生,因此我编写了一些代码,并尝试执行以下操作来在 TestTable 上启动查询,以基于 TestProperty 查找正确的实体。一旦找到正确的实体,我想使用它的特定属性来处理。

这是我当前代码遇到的错误:

TypeError: Cannot read property 'table' of undefined

我尝试查询第二个表的代码的一部分

var azureMobileApps = require('azure-mobile-apps'),
tables = require('azure-mobile-apps/src/express/tables'),
queries = require('azure-mobile-apps/src/query'),
logger = require('azure-mobile-apps/src/logger');

var table = azureMobileApps.table();

table.insert(function (context) {
    logger.info('Running TestTable1.insert');
    var testTable = azureMobileApps.tables.table('TestTable2');
    var query = queries.create('TestTable2').where({ TestProperty : context.item.testproperty }); 

    return context.execute()
        .then(function (results) {
        .....

最佳答案

由于Azure Table Storage是一项在云端存储非结构化NoSQL数据的服务,您可以引用https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-tables/以获得更多。

但是,azure-mobile-apps-node sdk 中的tables 模块包含将表添加到 Azure 移动应用程序的功能。它返回一个可以连接到 Express 应用程序的路由器,并具有一些用于注册表的附加功能。它实际上利用了 Azure SQL(Azure 上的 SQL Server 数据库服务)。

根据您的代码片段,您似乎正在实现第二个概念。

根据您的描述,如果我没有理解错的话,您想在 EasyTables 脚本中的 table1 操作中查询 table2

我们可以利用自定义中间件的“use()”来指定针对表的每个请求要执行的中间件,如azure-mobile-apps sdk文档http://azure.github.io/azure-mobile-apps-node/module-azure-mobile-apps_express_tables_table.html#~use上的描述.

EG

var queries = require('azure-mobile-apps/src/query');
var insertMiddleware = function(req,res,next){
    var table = req.azureMobile.tables('table2'),
    query = queries.create('table2')
            .where({ TestProperty : req.body.testproperty });
    table.read(query).then(function(results) {
        if(results){
            req.someStoreData = somehander(results); //some hander operations here to get what you want to store and will use in next step
            next();
        }else{
            res.send("no data");
        }
    });
};

table.insert.use(insertMiddleware, table.operation);
table.insert(function (context) {
   console.log(context.req.someStoreData);
   return context.execute();
});

此外,如果您需要在 EasyTables 脚本中推送通知,可以引用 Github 上的示例 https://github.com/Azure/azure-mobile-apps-node/blob/master/samples/push-on-insert/tables/TodoItem.js

关于javascript - Azure表存储的插入功能中查询其他表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35773617/

相关文章:

javascript - 无法在 map 单击监听器事件上显示另一个 map

javascript - 在 QML 中绘制 LineSeries 图表

javascript - 变量在 Angular $resource 函数的回调中突然变得未定义

node.js - Elastic Beanstalk CLI 错误 : This directory has not been set up with the EB CLI You must first run "eb init"

azure - 中继 WebHttp 中继绑定(bind)到 Web api 服务

Azure Bot 服务无法在代理后面的 Web 聊天中工作

javascript - 如何让 Polymer 元素占据页面上的所有垂直空间?

node.js - Cognito 登录 MFA 短信验证问题

Azure 专用端点和 Terraform

javascript - Node.JS 客户端应用模拟 1000/+ 客户端连接