问题
I am using Azure Table Storage to send out Push Notifications once a new item is inserted. Currently I am trying to query a second table to retrieve a string which I want to use for push notifications.
I am quite new to Node.js so I worked through some code and tried the following to start a query on the TestTable to find the correct entity with based on TestProperty. Once I have found the correct entity I want to use a specific property from it to work on with.
This is the error I get with my current code:
TypeError: Cannot read property 'table' of undefined
Part of my code where I try to query a second table
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) {
.....
回答1:
As Azure Table Storage is a service that stores unstructured NoSQL data in the cloud, you can refer to https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-tables/ to get more.
But, the tables module in azure-mobile-apps-node sdk contains functionality for adding tables to an Azure Mobile App. It returns a router that can be attached to an express app with some additional functions for registering tables. Which actually leverage Azure SQL (SQL Server database service on Azure).
According your code snippet, it seems you are implementing with the second concept.
And per your description, if I do not misunderstand, you want to query table2 in table1 operations in EasyTables scripts.
we can leverage "use()" to custom middleware to specify middleware to be executed for every request against the table as the description on the document of azure-mobile-apps sdk at http://azure.github.io/azure-mobile-apps-node/module-azure-mobile-apps_express_tables_table.html#~use.
E.G.
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();
});
Additionally, if you need to push notifications in EasyTables scripts, you can refer to the sample on Github at https://github.com/Azure/azure-mobile-apps-node/blob/master/samples/push-on-insert/tables/TodoItem.js
回答2:
Thanks Gary. It is very helpful. To complete your answer you can now write this like this :
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(); }
来源:https://stackoverflow.com/questions/35773617/query-other-tables-in-insert-function-of-azure-table-storage