类型'string[]'不可分配给nestjs中的类型'字符串' [英] Type 'string[]' is not assignable to type 'string' in nestjs

查看:42
本文介绍了类型'string[]'不可分配给nestjs中的类型'字符串'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过调用nestjs中的外部API来下载文件。 以下是我的服务代码

import { Injectable } from '@nestjs/common';
import * as fs from "fs";
import * as axios from "axios";
@Injectable()
export class FileService {
  saveFile() {
    return axios.default
      .get("https://www.nseindia.com/")
      .then((res) => {
        return axios.default.get(
          "https://www.nseindia.com/api/option-chain-indices?symbol=BANKNIFTY",
          {
            headers: {
              cookie: res.headers["set-cookie"],
            },
          }
        );
      })
      .then((res) => {
        //console.log(res.data);
        let data = JSON.stringify(res.data);
        fs.writeFileSync("../files/option-chain-indices.json", data);
        return data;
      })
      .catch((err) => {
        console.log(err);
      });
  }
}

我收到以下错误

error TS2322: Type 'string[]' is not assignable to type 'string'.

cookie: res.headers['set-cookie']

如何解决此问题?

推荐答案

是否可以按如下方式更改下面的代码行?

cookie: res.headers["set-cookie"] --> cookie: res.headers["set-cookie"].join(';')

这篇关于类型'string[]'不可分配给nestjs中的类型'字符串'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆