2017-08-25 16 views
0

我在我的應用服務上使用Azure Easy API。我正在嘗試一些,並且找不到適當的文檔。Azure Easy API - 找不到getTable('tableName')的文檔。插入

當我做了一個新的簡單的API,在頂部的評論說

// Use "request.service" to access features of your mobile service, e.g.: // var tables = request.service.tables;

所以我從那裏來找出我可以利用request.service.tables.getTable('tableName').insert({columnName: value})

我添加到我的任何表預期.insert()返回一個承諾,但它不。事實上,它似乎沒有任何回報。但我可以想象它是異步的。

由於它沒有回覆承諾,我下一個賭注是需要回撥,但是當我嘗試.insert({columnName: value}, function(r){response.send(r.toString()})時,整個API完全失敗。

我該如何使用這個.insert函數?

我在哪裏可以找到自己學習這些信息的文檔?谷歌搜索讓我無處可去。

+0

下面的答案包含'.then',這意味着這確實會返回一個承諾。當你測試你的代碼時,它返回什麼?你現有的代碼似乎沒有包含代碼來捕捉諾言(儘管我不是JS專家)。當你嘗試使用回調時,你得到了什麼錯誤? –

+0

下面的答案確實包含'.then',但它調用了不同的函數。我使用'request.service.tables.getTable()',而他使用'request.azureMobile.tables()'。當我測試代碼時,我很自然地在'insert'之後有'.then()',並且在日誌中它說'.then不存在於unedfined'之類的東西 - 顯然不是逐字的,類似於。我能夠確定'.insert()'實際上是返回undefined或null或類似的東西。 – TKoL

+0

只是說...完整的代碼示例和完整的錯誤消息使這些事情更容易解決。即完整的複製。 (雖然我不太可能自己解決這個問題) –

回答

1

這是一個code sample,您可以在Easy API中使用它將表中的記錄插入。

module.exports = { 
    "get": function (req, res, next) { 

     req.azureMobile.tables('tableName') 
      .insert({columnName: 'value'}) 
      .then(() => res.status(201).send('Success!')) 
      .catch(next); 
    } 
} 

app.js文件將包含以下內容。

// ---------------------------------------------------------------------------- 
// Copyright (c) 2015 Microsoft Corporation. All rights reserved. 
// ---------------------------------------------------------------------------- 

// This is a base-level Azure Mobile App SDK. 
var express = require('express'), 
    azureMobileApps = require('azure-mobile-apps'); 

// Set up a standard Express app 
var app = express(); 

// If you are producing a combined Web + Mobile app, then you should handle 
// anything like logging, registering middleware, etc. here 

// Configuration of the Azure Mobile Apps can be done via an object, the 
// environment or an auxiliary file. For more information, see 
// http://azure.github.io/azure-mobile-apps-node/global.html#configuration 
var mobileApp = azureMobileApps({ 
    // Explicitly enable the Azure Mobile Apps home page 
    homePage: true, 
    // Explicitly enable swagger support. UI support is enabled by 
    // installing the swagger-ui npm module. 
    swagger: true 
}); 

// Import the files from the tables directory to configure the /tables endpoint 
mobileApp.tables.import('./tables'); 

// Import the files from the api directory to configure the /api endpoint 
mobileApp.api.import('./api'); 

// Initialize the database before listening for incoming requests 
// The tables.initialize() method does the initialization asynchronously 
// and returns a Promise. 
mobileApp.tables.initialize() 
    .then(function() { 
     app.use(mobileApp); // Register the Azure Mobile Apps middleware 
     app.listen(process.env.PORT || 3000); // Listen for requests 
    }); 
+0

謝謝,我會盡快嘗試,並且如果我的工作成功,請將您的答案標記爲正確。 – TKoL

+0

嗯。沒有工作。也許是因爲我在舊版本的Azure上創建了這個應用程序服務。我檢查了我的app.js文件,沒有'require('azure-mobile-apps');',只有'require('azure-mobile-services');'沒關係,我的api通過SQL工作無論如何,猜測這必須足夠好。謝謝。看起來像天青移動服務已被棄用,但我不確定'遷移'是多麼可行。 Hrm ... – TKoL

+0

如果實際上不工作,請不要標記爲正確! –