Azure:无法在exports.post中调用未定义的方法'then'

时间:2015-11-14 17:32:44

标签: javascript azure mobile service promise

我正在使用azure移动服务,使用以下自定义API:

var returnVal = new Object;


exports.post = function (request, response) {
    // Use "request.service" to access features of your mobile service, e.g.:
    //   var tables = request.service.tables;
    //   var push = request.service.push;

    var merchantdetailsTable = request.service.tables.getTable("merchantdetails");
    var resourceName;
    //console.log(JSON.stringify(request.parameters));
    merchantdetailsTable.insert({
        name: request.body.workerNameInput,
        emailid: request.body.workerEmailIDInput,
        contact: request.body.workerContactNumberInput
    }).then(function (merchantInserted) {
        returnVal.workerId = merchantInserted.id;
        resourceName = returnVal.workerId.toLowerCase();
        var shopworkersTable = request.service.tables.getTable("shopworkers");
        return shopworkersTable.insert({
            id: merchantInserted.id,
            shopid: request.body.shopId
        });
    }, function(err){
        return response.send(statusCodes.INTERNAL_SERVER_ERROR, err);
    }).then(function () {
        var accountName = appSettings.STORAGE_ACCOUNT_NAME;
        var accountKey = appSettings.STORAGE_ACCOUNT_ACCESS_KEY;
        var host = accountName + '.blob.core.windows.net';
        var blobService = azure.createBlobService(accountName, accountKey, host);
        return blobService.createContainerIfNotExists("merchant-image", { publicAccessLevel: 'blob' });
    }, function (err) {
        return response.send(statusCodes.INTERNAL_SERVER_ERROR, err);
    }).then(function(error){
        if (!error) {
            // Provide write access to the container for the next 5 mins. 
            var sharedAccessPolicy = {
                AccessPolicy: {
                    Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
                    Expiry: new Date(new Date().getTime() + 5 * 60 * 1000)
                }
            };

            // Generate the upload URL with SAS for the new image.
            var sasQueryUrl =
         blobService.generateSharedAccessSignature("merchant-image",
         '', sharedAccessPolicy);
            // Set the query string.
         returnVal["merchantImage"].sasQueryString = qs.stringify(sasQueryUrl.queryString);

            // Set the full path on the new new item, 
            // which is used for data binding on the client. 
         returnVal["merchantImage"].imageUri = sasQueryUrl.baseUrl + sasQueryUrl.path + '/'
                + resourceName;

         var accountName = appSettings.STORAGE_ACCOUNT_NAME;
         var accountKey = appSettings.STORAGE_ACCOUNT_ACCESS_KEY;
         var host = accountName + '.blob.core.windows.net';
         var blobService = azure.createBlobService(accountName, accountKey, host);
         return blobService.createContainerIfNotExists("pharmacy-certificate", { publicAccessLevel: 'blob' });
        }
        else {
            return response.send(statusCodes.INTERNAL_SERVER_ERROR);
        }
    }, function (err) {
        return response.send(statusCodes.INTERNAL_SERVER_ERROR, err);
    }).done(function (error) {

        if (!error) {
            // Provide write access to the container for the next 5 mins. 
            var sharedAccessPolicy = {
                AccessPolicy: {
                    Permissions: azure.Constants.BlobConstants.SharedAccessPermissions.WRITE,
                    Expiry: new Date(new Date().getTime() + 5 * 60 * 1000)
                }
            };

            // Generate the upload URL with SAS for the new image.
            var sasQueryUrl =
         blobService.generateSharedAccessSignature("pharmacy-certificate",
         '', sharedAccessPolicy);
            // Set the query string.
            returnVal["pharmacyCertificate"].sasQueryString = qs.stringify(sasQueryUrl.queryString);

            // Set the full path on the new new item, 
            // which is used for data binding on the client. 
            returnVal["pharmacyCertificate"].imageUri = sasQueryUrl.baseUrl + sasQueryUrl.path + '/'
                   + resourceName;
            response.send(statusCodes.OK, returnVal);
        }
        else {
            return response.send(statusCodes.INTERNAL_SERVER_ERROR);
        }
    }, function (err) {
        return response.send(statusCodes.INTERNAL_SERVER_ERROR, err);
    });

    response.send(statusCodes.OK, { message : 'Hello World!' });
};

exports.get = function(request, response) {
  response.send(statusCodes.OK, { message : 'Hello World!' });
};

我收到以下错误:

  

TypeError:无法调用未定义的方法'then'       在exports.post(D:\ home \ site \ wwwroot \ App_Data \ config \ scripts \ api \ addWorker.js:17:8)

1 个答案:

答案 0 :(得分:0)

Azure移动服务不会从表操作返回承诺。您需要传递包含成功和错误回调的选项对象,如https://msdn.microsoft.com/en-us/library/azure/jj554210.aspx所述。

我强烈建议您查看该产品的新实施,Azure移动应用程序 - https://www.npmjs.com/package/azure-mobile-apps。 (免责声明:我在Azure移动应用团队中为Microsoft工作)