简体   繁体   English

Azure Node.js应用服务-创建自定义承诺

[英]Azure Node.js App Service - Create custom promise

I am working on an Azure App Service (Mobile App) project with a Node.js backend. 我正在使用带有Node.js后端的Azure应用服务(移动应用)项目。

I have multiple user levels and certain API methods should be off-limits to non-elevated users. 我有多个用户级别,某些API方法应禁止非高级用户使用。 Basically, I have 3 levels of user: 基本上,我有3个级别的用户:

  • Normal user 普通用户
  • Super user 超级用户
  • Internal user (employee) 内部用户(员工)

I have built code that functions correctly to check a user's status and prevent the API method from being used. 我构建的代码可以正常运行,以检查用户状态并阻止使用API​​方法。 Because this code is in use across many of my API methods, I am trying to create a shared code module so that I do not have the same code repeated 20+ times throughout my API. 由于此代码已在我的许多API方法中使用,因此我试图创建一个共享代码模块,以使在整个API中不会重复重复20多次以上相同的代码。

The problem I am running into is that I can't figure out how to correctly return a promise from my shared code that will resolve with a boolean value. 我遇到的问题是我无法弄清楚如何正确地从共享代码中返回一个用布尔值解析的promise。

Here's an instance of what I'm trying to do (non-functional code): 这是我要执行的操作的一个实例(非功能代码):

module.exports = {
    isEmployee: function(req, next) {
        var context  = req.azureMobile;
        var user     = context.user;
        var username = user.id;
        var query    = {
            sql        : 'Exec usp_...',
            parameters : [ { name: '...', value: ... } ]
        };

        // The following line has compiler error
        return new Promise(function (fulfill, reject) {
            context.data.execute(query).then(function (results) {
                var result = results.Result;
                if (result === 'Success') {
                    fulfill(true);                  
                } else {
                    fulfill(false);                 
                }
            }).catch(next);         
        });     
    }
};

I got to this code by attempting to adapt code I found here . 我通过尝试修改此处找到的代码来获得此代码。

I am getting a compiler error of "Cannot find name 'Promise'". 我收到“找不到名称'Promise'”的编译器错误。

So, basically, I'm stuck on how to construct my own promise in this shared code method so that my other methods that call this method can await its running. 因此,基本上,我坚持如何在此共享代码方法中构造自己的Promise,以便其他调用此方法的方法可以等待其运行。 I guess I'm trying to make this method "thenable". 我想我正在尝试使此方法“可以”。 How can I do this on Azure Node.js? 如何在Azure Node.js上执行此操作?

Most probably, the version of V8 used by your nodejs version (intern the nodejs version itself) is not supporting the Promise constructor yet 最有可能的是,您的nodejs版本使用的V8版本(实习生nodejs版本本身)尚不支持Promise构造函数

Can you check the version of v8 used by the node version installed in the machine ? 您可以检查机器中安装的节点版本使用的v8版本吗?

you can do this using node -e 'console.log(process.versions.v8);' 您可以使用node -e 'console.log(process.versions.v8);'执行此操作 .

but in my opinion you should install npm module es6-promise and be done with it 但我认为您应该安装npm模块es6-promise并完成此操作

install : npm install es6-promise install: npm install es6-promise

use : var Promise = require('es6-promise').Promise; 使用: var Promise = require('es6-promise').Promise;

It seems you forget to install the Promise module in your mobile app. 似乎您忘记了在移动应用程序中安装Promise模块。

You can simply use the Visual Studio Online "Monaco" (the online editor when you editing Easy APIs or Easy Tables). 您可以简单地使用Visual Studio Online“ Monaco”(编辑Easy API或Easy Tables时的在线编辑器)。 Click Open the Console button, run the command npm install promise --save to install the module in your mobile app and configure it into the package.json file. 单击“ 打开控制台”按钮,运行命令npm install promise --save将模块安装在您的移动应用程序中,并将其配置到package.json文件中。 在此处输入图片说明

Then you can create a shared script in the directory, and invoke it in the Easy APIs scripts. 然后,您可以在目录中创建共享脚本,然后在Easy API脚本中调用它。 For the quick reference, I build a simple structure: 为了快速参考,我构建了一个简单的结构:
在此处输入图片说明

I create the shared script with promise functionality: 我创建带有promise功能的共享脚本:

var Promise = require('promise');
exports = module.exports = function customP(context) {
    var query = {
        sql: 'SELECT * from TodoItem',
    };
    return new Promise(function (fulfill, reject) {
        context.data.execute(query).then(function (results) {
            if (results.length > 0) {
                fulfill(true);                  
            } else {
                fulfill(false);                 
            }
        })       
    });
}

And invoked in the Easy API script api/test.js : 并在Easy API脚本api/test.js

var customP = require('../customP');
module.exports = {
    "get":function(req,res,next){
        customP(req.azureMobile).then(function(data){
            if(data){
                res.json("it's true");
            }else{
                res.json("it's false")
            }
        })
    }
}

Any further concern, please feel free to let me know. 如有任何其他疑问,请随时告诉我。

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

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