2016-10-10 19 views
0

これは初めての使用です。私は最近、新しいMicrosoft Azure App Serviceに移行したため、node.jsの構文などが変更されたか、少し違っているようです。これは古いモバイルサービスの私の現在のコードなので、私は新しい構文に移行したいと思っています。変更などの点では非常に限られた参考文献があり、私はGoogle上でもそれほど多く見つけることができませんでした。AzureモバイルアプリCRUD操作の構文

function insert(item, user, request) { 
var table = tables.getTable('user'); 
table.where({ 
    userid: item.userid 
}).read({ 
    success: upsertItem 
}); 


function upsertItem(existingItems) { 
    if (existingItems.length > 0) { 
     item.id = existingItems[0].id; 
     table.update(item, { 
     success: function(updatedItem) { 
     request.respond(200, updatedItem) 
     } 
     }); 
    } else { 
     request.execute(); 
    } 
} 

}

私の目的はUPSERTを実行することで、新しいスクリプトのこのISAサンプル

var table = module.exports = require('azure-mobile-apps').table(); 

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

IVEにも、この記事を見たが、

Azure mobile apps CRUD operations on SQL table (node.js backend)

その有用ではありません私の元のコードに従って。あなたが正確な変換を提供することによって私を助けることができるなら、それは私の好みの答えでしょう。

ご協力いただきありがとうございます。

答えて

0

一般に、use()を独自のミドルウェアにカスタマイズして追加の操作を処理することができます。

As挿入操作は、HTTP POST要求を実装します。 Azure Mobile Appsに対するPOSTMANのデータを含むポストリクエストの実装方法については、Azure Mobile Apps - An item to insert was not providedを参照してください。

次に、Azure Mobile Appsにbody-parserモジュールをインストールして、リクエストの本文を解析することができます。

は、モジュールを有効にするには、app.jsに次のコードを追加します。最後

var bodyParser = require('body-parser'); 
app.use(bodyParser.json({ limit: '50mb' })); 
app.use(bodyParser.urlencoded({ limit: '50mb', extended: true })); 

、ミドルウェアを使用する方法について簡単にテーブルのスクリプトに次のコードスニペットを考えてみてください。

var insertMiddleware = function(req,res,next){ 
    var queries = require('azure-mobile-apps/src/query'); 
    var t = req.azureMobile.tables('TodoItem'); 
    var query = queries.create('TodoItem') 
      .where({ text: req.body.text }); 
    t.read(query).then(function(data){ 
     if(data.length === 0){ 
     next(); 
     }else{ 
     data.text = "something new"; 
     console.log(data); 
     var query = { 
      sql: 'UPDATE TodoItem SET text = @text where id = @id', 
      parameters: [ 
       { name: 'text', value: data.text }, 
       { name: 'id', value: data.id } 
      ] 
     }; 
     req.azureMobile.data.execute(query).then(function(result){ 
      res.status(200).json("success"); 
     }); 
     } 
    }); 
}; 
table.insert.use(insertMiddleware, table.operation); 
table.insert(function (context) { 
    return context.execute(); 
}); 
0

私はここに新しいNode.jsのSDKに完全なシリーズをやった:https://shellmonger.com/30-days-of-azure-mobile-apps-the-table-of-contents/

は、公式ドキュメントもあり、それはかなり包括的です:https://azure.microsoft.com/en-us/documentation/articles/app-service-mobile-node-backend-how-to-use-server-sdk/

がある互換パッケージもありますhttps://www.npmjs.com/package/azure-mobile-apps-compatibility

これらの間で、簡単に新しいAzure Mobile Apps SDKにサイトを変換できるはずです。