在 Azure 表存储的插入函数中查询其他表
Query other tables in insert function of Azure Table Storage
我正在使用 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 表存储是一项将非结构化 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 脚本中的table1
操作中查询table2
。
我们可以利用自定义中间件的"use()"来指定要针对表的每个请求执行的中间件,如 Azure 移动应用 SDK 文档在 http://azure.github.io/azure-mobile-apps-node/module-azure-mobile-apps_express_tables_table.html#~use 上的描述。
例如
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
谢谢加里。这是非常有帮助的。为了完成你的答案,你现在可以这样写:
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(); }
- 如何将参数赋予作为查询参数的参数值给定的函数
- j查询关于回调函数的术语
- 如何在函数带有更多postJSON查询时使用jQuery
- j查询如何在加载了 load() 函数的页面上进行 POST/AJAX,而无需刷新页面
- j查询限制文本函数
- j查询函数导入
- 如何委派查询函数
- j查询函数不在胡子中触发
- j查询函数在第一次点击时不触发
- j查询函数范围混淆
- 猫鼬,函数不能在未定义的、来自查询函数上执行
- j查询函数和自调用匿名函数之间的区别
- 编写mongodb REST API的通用查询函数
- 带有Select2的FormValidation未捕获异常:未为Select2定义查询函数
- 如何更好地优化这些数据库查询/函数流
- 不能在查询函数中访问自变量
- Javascript自执行查询函数
- j查询函数同步调用
- j单击时查询函数未被类多次工作
- “未为 Select2 未定义错误定义查询函数”