小编ann*_*123的帖子

了解tsconfig文件中的esModuleInterop

我正在检查某人的.tsconfig文件,发现了--esModuleInterop

这是他的.tsconfig档案

{
  "compilerOptions": {
    "moduleResolution": "node",
    "target": "es6",
    "module": "commonjs",
    "lib": ["esnext"],
    "strict": true,
    "sourceMap": true,
    "declaration": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "declarationDir": "./dist",
    "outDir": "./dist",
    "typeRoots": ["node_modules/@types"]
  },
  "include": ["src/**/*.ts"],
  "exclude": ["node_modues"]
}
Run Code Online (Sandbox Code Playgroud)

在这里,我的主要问题是 "esModuleInterop": true,"allowSyntheticDefaultImports": true,。我知道他们有点依赖 "module": "commonjs",。有人可以尝试用最好的人类语言来解释它吗?

allowSyntheticDefaultImports州的官方文档

允许从模块进行默认导入而没有默认导出。这不影响代码发出,仅影响类型检查。

那是什么意思?如果没有任何导出默认值,我认为导入默认值的唯一用例就是初始化某些东西?喜欢单身人士吗?

以下问题/答案也没有意义, 是否可以在tsconfig中使用--esModuleInterop而不是将其作为标志?

--esModuleInterop在编译器页面上定义

发出__importStar和__importDefault帮助程序以实现运行时babel生态系统兼容性,并启用--allowSyntheticDefaultImports以实现类型系统兼容性。

我似乎也很难理解/理解

typescript

31
推荐指数
2
解决办法
1万
查看次数

当您不知道键时访问对象内部的某些内容

我得到以下对象

{
  IuW1zvaSABwH4q: {
    label: 'Random Image of TypeScript not relavent to coworking',
    thumbId: 'd501-f-b601-c8b1-4bd995e',
    schemaType: 'xman-assets-image-set'
  }
}
Run Code Online (Sandbox Code Playgroud)

现在,我想访问其中的thumbID的值,即d501-f-b601-c8b1-4bd995e

但是我的根密钥似乎是动态/随机的(IuW1zvaSABwH4q),如何访问其中的值?

javascript

22
推荐指数
4
解决办法
1636
查看次数

用Axios承诺所有人

我刚读了一篇与promise相关的文章,无法理解我们如何使用Axise通过Promise.all进行多个API调用.

所以考虑有3个URL,让我们这样称呼它

let URL1 = "https://www.something.com"
let URL2 = "https://www.something1.com
let URL3 = "https://www.something2.com"
Run Code Online (Sandbox Code Playgroud)

还有一个我们将存储Value的数组

  let promiseArray = []
Run Code Online (Sandbox Code Playgroud)

现在,我想并行运行(Promise.all),但我无法确定我们将如何做到这一点?因为axios本身就有一个承诺(或者至少我是如何使用它的).

axios.get(URL).then((response) => {
}).catch((error) => {
})
Run Code Online (Sandbox Code Playgroud)

问:有人可以告诉我我们如何使用promise.all和axios发送多个请求

javascript promise axios

20
推荐指数
6
解决办法
2万
查看次数

Unknown compiler options include & exclude

I was making my first typescript-node-express application.

To start with, I created my own tsconfig file which looks like

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs", 
    "strict": true,
    "baseUrl": "./",
    "outDir": "./build",
    "sourceMap": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "importHelpers": true,
    "types": [
      "node"
    ],
    "typeRoots": [
        "node_modules/@types"
    ],
    "include": [
      "src/**/*.ts"
    ],
    "exclude": [
      "node_modues"
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

And inside my src/app.ts I am initialising my express app

import * as express from "express";

class App {

  constructor() {
    this.app …
Run Code Online (Sandbox Code Playgroud)

javascript node.js typescript

17
推荐指数
2
解决办法
8233
查看次数

UnhandledPromiseRejectionWarning:此错误是由于将异步函数内部抛出而没有catch块引起的

我的Node-Express应用程序出现以下错误

UnhandledPromiseRejectionWarning:未处理的承诺拒绝。该错误是由于在没有catch块的情况下抛出异步函数而引起的,或者是由于拒绝了未经.catch()处理的诺言而引起的。(拒绝ID:4)

至少可以说,我创建了一个类似于以下内容的辅助函数

const getEmails = (userID, targettedEndpoint, headerAccessToken) => {
    return axios.get(base_url + userID + targettedEndpoint,  { headers: {"Authorization" : `Bearer ${headerAccessToken}`} })
    .catch(error => { throw error})
}
Run Code Online (Sandbox Code Playgroud)

然后我导入此辅助函数

const gmaiLHelper = require("./../helper/gmail_helper")
Run Code Online (Sandbox Code Playgroud)

然后像这样在我的api路由中调用它

router.get("/emailfetch", authCheck, async (req, res) => {
  //listing messages in users mailbox 
  let emailFetch = await gmaiLHelper.getEmails(req.user._doc.profile_id , '/messages', req.user.accessToken)
  .catch(error => { throw error})
  emailFetch = emailFetch.data
  res.send(emailFetch)
})
Run Code Online (Sandbox Code Playgroud)

从我的角度来看,我认为我正在通过使用catch块来处理错误。

问题:有人可以向我解释为什么我得到此错误以及如何解决该错误?

javascript node.js express

13
推荐指数
3
解决办法
3万
查看次数

Javascript 类与打字稿类

我是 TypeScript 和 Javascript 类的新手!

我正在学习 TypeScript,在那里我创建了一些像这样简单的东西

class User {
  name: string;
  email: string;

  constructor(name: string, email: string) {
    this.name = name;
    this.email = email;
  }
}

let newUser = new User("Rohit Bhatia", "iro@gmail.com");
Run Code Online (Sandbox Code Playgroud)

这是给我的等价物

var User = /** @class */ (function () {
    function User(name, email) {
        this.name = name;
        this.email = email;
    }
    return User;
}());
var newUser = new User("Rohit Bhatia", "iro@gmail.com");
Run Code Online (Sandbox Code Playgroud)

现在,我有三个问题

  1. 什么是@class(或 js 中的一般@)? var User = /** @class */ (function () {

  2. 类也在 javascript …

javascript typescript

11
推荐指数
2
解决办法
5200
查看次数

iterating and storing object equal to itself

Can someone help me in understanding the reason why someone would iterate when it's value is equal to it?

for example

let subStyle = {
      width: 300,
      height: 50,
      borderRadius: 20,
      backgroundColor:  theme.primaryBlue
    };

subStyle = {
        ...subStyle,
        backgroundColor: theme.primaryWhite,
        borderWidth: 2,
        borderColor: '#707070'
      }
Run Code Online (Sandbox Code Playgroud)

i.e in the above code, what could be the reason for doing the same?

subStyle = {
        ...subStyle,
        backgroundColor: theme.primaryWhite,
        borderWidth: 2,
        borderColor: '#707070'
      }
Run Code Online (Sandbox Code Playgroud)

javascript

10
推荐指数
1
解决办法
99
查看次数

如何在GraphQL中处理错误并发送响应

我从GraphQL开始,却无法理解如何在GraphQL中引发错误

我在网上浏览了几篇文章,但是几乎所有文章都使用Apollo,并且代码结构看起来与我的工作方式大不相同。

考虑这段代码,在这里我要进行突变,现在如何发送有错误的响应消息并在发生错误的情况下更改标头状态消息?

  AddNewPersonalInfo: {
  type: userDashboardType,
  args: { 
    parameter: {
      type: userCreationlInputType
    }
  }, 
  resolve: async (parent, args, context) => {
    args.parameter.userId = context.req.headers.userId
    //Check if user info already exsist
    const checkIfUserInformationExsist = await getSelectedThingFromTable('CatsWork_personal', 'userId', `${userId}`)
    if (checkIfUserInformationExsist[0]) {
      const error = {
        code: 403, 
        message: 'User info Already exsist'
      }
      throw new Error(error)
    } else {
      try {
      const addLinkedinUser = await insertIntheTable('personal', payload)
      return true
      } catch (err) {
        console.error(err)
        throw new Error(err)
      }
    }
  } …
Run Code Online (Sandbox Code Playgroud)

javascript node.js graphql

9
推荐指数
2
解决办法
433
查看次数

React-Redux中错误处理的正确方法

我想了解使用React-Redux进行错误处理的更通用或更正确的方法。

假设我有电话号码注册组件。

如果输入的电话号码无效,该组件将引发错误提示

处理该错误的最佳方法是什么?

想法1: 创建一个组件,该组件接受错误并在将错误传递给它时调度一个操作

想法2: 由于错误与该组件有关,请将该错误传递给组件(该组件未连接到redux,即错误处理程序组件将不会分派操作)

问题:有人可以指导我在React-Redux中针对大型应用程序进行错误处理的正确方法吗?

reactjs redux

8
推荐指数
1
解决办法
230
查看次数

Firebase 函数环境变量

所以我正在将我的应用程序从 node express 迁移到 firebase-functions!

在我的 node-express 应用程序中,我有 .env 文件,其中包含所有数据,对于初学者,让我们将其视为我的 .env 文件

GOOGLE_CLIENT_ID = 4046108-bssbfjohpj94l0dhpu69vpgs1ne0.apps.googleusercontent.com
GOOGLE_CLIENT_SECRET = lTQHpj3yY57oQpO
Run Code Online (Sandbox Code Playgroud)

然后在我的护照策略中,我有这样的事情

passport.use(new GoogleStrategy({
    clientID: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    callbackURL:  "/auth/google/callback",
    userProfileURL: 'https://www.googleapis.com/oauth2/v3/userinfo',
    accessType: 'offline',
    passReqToCallback: true
  },
Run Code Online (Sandbox Code Playgroud)

现在,

问题:1 - Firebase 函数可能不支持 .env 文件,所以我们可以设置 env 变量而不使用 set 标志手动添加它吗?假设我的环境中有很多变量

问题 - 2:我可以通过执行这样的操作来访问我设置的变量吗

firebase functions:config:set slack.url=https://hooks.slack.com/services/XXX
Run Code Online (Sandbox Code Playgroud)

使用

 process.env.slack.url
Run Code Online (Sandbox Code Playgroud)

或者我们必须做(必要的)

functions.config().slack.url
Run Code Online (Sandbox Code Playgroud)

问题:3Firebase Docs 中,它是这样写的

在函数运行时和本地模拟函数中会自动填充环境变量,包括:

process.env.GCLOUD_PROJECT:提供 Firebase 项目 ID

process.env.FIREBASE_CONFIG:提供以下 Firebase 项目配置信息:

他们的意思是什么?如果第二个问题的答案是错误的,那么他们如何使用 process.env.FIREBASE_CONFIG:

node.js firebase google-cloud-functions

7
推荐指数
1
解决办法
3553
查看次数