简体   繁体   English

在Azure表脚本中为Azure移动应用插入不同的表

[英]Insert in a different table in Azure table script for Azure Mobile Apps

I've been searching extensively for this answer but only found solutions referring to the old Azure App Services instead of the new Azure Mobile Apps. 我一直在广泛寻找此答案,但仅找到解决方案,该解决方案是指旧的Azure应用服务而不是新的Azure移动应用。

In this exemple, in the Table Scripts of a table, I'm inserting the authenticated user id into the userID field of that same table. 在此示例中,在表的表脚本中,我将经过身份验证的用户ID插入同一表的userID字段中。

table.insert(function (context) {
   context.item.userId = context.user.id;
   return context.execute();
});

But if also want to insert it in a different table from the one the user has asked to insert? 但是,如果还要将其插入与用户要求插入的表不同的表中吗? How can I access this other table from the Table Scripts? 如何从表脚本访问该其他表?

Before, as shown on all solutions I found, you could do "tables.getTable('otherTable')" to access another table from the same SQL DB. 之前,如我在所有发现的解决方案上所示,您可以执行“ tables.getTable('otherTable')”来访问同一SQL DB中的另一个表。 But now I get an error. 但是现在我得到一个错误。

Reading the 'Azure Mobile Apps - Node SDK' documentation, I found the "context.tables()" function, that appears to obtain a different table from the db. 阅读“ Azure移动应用程序-节点SDK”文档后,我发现了“ context.tables()”函数,该函数似乎从数据库中获取了不同的表。 But when I insert using the code bellow nothing happens: 但是当我使用下面的代码插入时,什么也没发生:

table.insert(function (context) {
    var table2 = context.tables('table2');
    table2.insert({string_column: '1234'});
    return context.execute();
});

Thanks! 谢谢!

Found out the correct way of doing it. 找到正确的方法。 Here is my code: 这是我的代码:

var insertMiddleware = function(req,res,next){

    var table2 = req.azureMobile.tables('table2');
    var toBeInserted = {field1: 'something',
                        id: '1'}; //id is needed 

    var promiseReturned = table2.insert(toBeInserted);
    promiseReturned.then(function(data){
        console.log('Inserted with success!');
        next();
    }, function(error) {
        console.log('Found an error', error);
    });
};

table.insert.use(insertMiddleware, table.operation);

table.insert(function (context) {
   return context.execute();
});

First I declare a middleware function that will be executed every time the insert is called. 首先,我声明一个中间件函数,该函数将在每次调用插入函数时执行。 In it I call the insert function from the table I want to access, and it returns a promise. 在其中,我从要访问的表中调用插入函数,并返回一个Promise。 (that was my error) (那是我的错误)

Then I associate the middleware with the insert function of the table where the script is running using the "use" function. 然后,我使用“ use”功能将中间件与运行脚本的表的插入功能相关联。

Finally I run the insert function normally. 最后,我正常运行插入功能。

In my case I wanted to insert the a record into another table after the id was generated so I wrote it as part of the callback. 就我而言,我想在生成ID后将一条记录插入另一个表中,因此我将其写为回调的一部分。

table.insert(function(context) {
    return context.execute().then(function(results) {
        var table2 = context.tables('table_name');
        var newRecord = {
            id: results.id,
            some: "data",
            more: "123"
        };

        table2.insert(newRecord);

        return results;
    });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM