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

问题描述 投票:0回答:2

插入新项目后,我使用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) {
        .....
javascript node.js azure azure-table-storage azure-mobile-services
2个回答
2
投票

由于Azure表存储是一种在云中存储非结构化NoSQL数据的服务,因此您可以参考https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-tables/获取更多信息。

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

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

根据您的描述,如果我没有误解,您想在EasyTables脚本中查询table2中的table1

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

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脚本中推送通知,可以参考https://github.com/Azure/azure-mobile-apps-node/blob/master/samples/push-on-insert/tables/TodoItem.js上的Github上的示例


0
投票

谢谢加里。这非常有帮助。要完成您的答案,您现在可以这样写:

async function filterByAllowedDomain(context) {
  var domains = await context.tables('domains')
    .where({ allowed: true })
    .read();

  var categories = await context.tables('categories')
    .where(function (ids) {
        return this.domainId in ids;
    }, domains.map(d => d.id))
    .read();

  context.query.where(function (ids) {
    return this.categoryId in ids;
  }, categories.map(c => c.id));

  return context.execute(); }
© www.soinside.com 2019 - 2024. All rights reserved.