尝试执行简单的http请求时出现JS错误错误

吕迪格

我正在尝试与网关交互并想尝试https://www.npmjs.com/package/@types/request模块。

我试图执行的代码:

export class OAuthAccessor {

    //some stuff

    public static createOAuthAccessToken() {
        this.makeCall();  //this line 11
    }

    private static makeCall() {
        var request = require('request');
        request.post({
            uri: "https://api.sandbox.paypal.com/v1/oauth2/token",
            headers: {
                "Accept": "application/json",
                "Accept-Language": "en_US",
                "content-type": "application/x-www-form-urlencoded"
            },
            auth: {
                'user': this.clientID,
                'pass': this.secret,
                // 'sendImmediately': false
            },
            form: {
                "grant_type": "client_credentials"
            }
        }, function(error, response, body) {
            console.log("error = " + error);
            console.log(body);
        });
    }

}

错误:

file:///.../OAuthAccessor.js:11:30: JS 错误错误:找不到模块“请求”。计算路径'/Users/admin/Library/Developer/CoreSimulator/Devices/ACA260DA-C0FD-4373-8822-4474222199751/data/Containers/Bundle/Application/EE654C43-B629-4817-9252F-appaG4-9252F-AG4EIntegration tns_modules/请求'

我的 Package.json:

{
  "description": "NativeScript Application",
  "license": "SEE LICENSE IN <your-license-filename>",
  "readme": "NativeScript Application",
  "repository": "<fill-your-repository-here>",
  "nativescript": {
    "id": "org.nativescript.IPGIntegration",
    "tns-ios": {
      "version": "3.1.0"
    },
    "tns-android": {
      "version": "3.1.1"
    }
  },
  "dependencies": {
    "@types/request": "^2.0.0",
    "nativescript-theme-core": "~1.0.2",
    "require": "^2.4.20",
    "tns-core-modules": "~3.1.0"
  },
  "devDependencies": {
    "babel-traverse": "6.25.0",
    "babel-types": "6.25.0",
    "babylon": "6.17.4",
    "lazy": "1.0.11",
    "nativescript-dev-typescript": "~0.5.0",
    "typescript": "~2.3.4"
  }
}

我的 tsconfig.json:

{
    "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "noEmitHelpers": true,
        "noEmitOnError": true,
        "lib": [
            "es6",
            "dom"
        ],
        "baseUrl": ".",
        "paths": {
            "*": [
                "./node_modules/tns-core-modules/*",
                "./node_modules/@types/request/*"
            ]
        }
    },
    "exclude": [
        "node_modules",
        "platforms",
        "**/*.aot.ts"
    ]
}

我做了什么:用打字稿模板创建一个新项目

tns create my-app-name --template typescript
npm install typescript (version 2.3.4)
npm install @types/request

我已经尝试重新安装打字稿和类型/请求模块和 node_modules 文件夹,以及其他版本的打字稿。安装该模块时我错过了什么吗?

普卡涅夫

你不能使用requestnpm 模块,因为它是一个依赖于内置节点功能的 nodeJS 模块,比如net模块。

为了发出 http 请求,您可以使用跨平台模块的http实现,如 NativeScript 文档文章中所示 - http://docs.nativescript.org/cookbook/http

如果您需要发出 https 请求,您可以使用 nativescript-https 插件 - https://www.npmjs.com/package/nativescript-https

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

尝试发布大型JSON数据时出现错误的请求错误

尝试执行独特的归约时出现错误(Reducer :)

发出HTTP请求时出现“错误:getaddrinfo ENOTFOUND”错误

尝试更改最大请求长度时出现HTTP错误500.19

(400)尝试将简单值发布到API时出现错误请求

尝试绘制简单的条形图时出现错误

尝试执行SQL查询时出现无效的对象错误

尝试执行表单验证时出现错误

尝试从vue.js中的HTTP请求推送到数组时出现错误

尝试在flutter上获取请求时出现错误

尝试POST请求时出现猫鼬验证错误

尝试POST请求时出现节点js错误

尝试提交表单时出现HTTP发布请求错误

尝试在Angular中执行Delete操作时出现错误

尝试执行pip安装时出现错误

错误:发出HTTPS / HTTP请求时出现getaddrinfo ENOTFOUND错误

尝试执行Sql命令时出现错误

发出HTTP请求时出现OpenSSL错误

尝试简单的单例示例时出现模板Haskell错误

尝试使用SolidWorks VBA发出HTTP请求时出现奇怪的错误

当我尝试在节点中执行https发布请求时,出现getaddrinfo ENOTFOUND错误

尝试执行删除查询时出现错误

HTTP请求失败!尝试从youtube获取内容时出现HTTP / 1.0 400错误请求

尝试在 lighttpd.config 中执行 HTTP->HTTPS 时出现拒绝连接错误

HTTP 状态 400 – 尝试将日期保存到数据库时出现错误请求

尝试从 DocumentDb 中删除文档时出现错误请求错误

尝试执行 unwindsegue 时出现错误 UIAlert

尝试使用角度 UI 执行获取请求时出现 base 64 图像未定义错误

尝试向pypi api发出请求时出现403错误