Reputation: 249
I am trying to download data into JSON file from URL in nestjs but I cannot figure out how to do it.
Below is my controller function with reference to this answer.
export class DataController {
constructor(private readonly httpService: HttpService) { }
@Get()
async download(@Res() res) {
const writer = createWriteStream('user.json');
const response = await this.httpService.axiosRef({
url: 'https://api.github.com/users/prasadg',
method: 'GET',
responseType: 'stream',
headers: {
'Content-Type': 'application/json',
}
});
response.data.pipe(writer); <--- Error here: Property 'pipe' does not exist on type 'unknown'.
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
As I mentioned above it is showing error
error TS2339: Property 'pipe' does not exist on type 'unknown'
Expected result : I want to write json data into file (user.json) in my folder on server.
How do I solve this problem?
Upvotes: 0
Views: 2689
Reputation: 815
This is just a types mistake, make sure the axios is in your project by this command:
npm i axios
Then just declare the type in your response variable
@Get()
async download() {
const writer = createWriteStream('user.json');
// response variable has to be typed with AxiosResponse<T>
const response: AxiosResponse<any> = await this.httpService.axiosRef({
url: 'https://api.github.com/users/prasadg',
method: 'GET',
responseType: 'stream',
});
response.data.pipe(writer);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
Upvotes: 1
Reputation: 120
This is untested and I'm not sure it works. But there are a few misunderstandings in your code I can hopefully clear up and put you in the right direction.
export class DataController {
constructor(private readonly httpService: HttpService) { }
@Get()
async download(@Res() res) {
const writer = createWriteStream('user.json');
// Here you await the result of the request
// Meaning you wait here until all the data is recieved, which is then stored in response
const response = await this.httpService.axiosRef({
url: 'https://api.github.com/users/prasadg',
method: 'GET',
responseType: 'stream',
headers: {
'Content-Type': 'application/json',
}
});
// Here you are trying to use a function called pipe on the property data on the response
response.data.pipe(writer); <--- Error here: Property 'pipe' does not exist on type 'unknown'.
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
I imagine you would rather be doing something like this for the data pipe
writer.pipe(response.data);
If you are using fs.createWriteStream
it will probably look more like this
writer.write(response.data);
You probably will want to put the request into the stream, so the data can be streamed as it is recieved, rather than having to await it
export class DataController {
constructor(private readonly httpService: HttpService) { }
@Get()
async download(@Res() res) {
const writer = createWriteStream('user.json');
writer.pipe(this.httpService.axiosRef({
url: 'https://api.github.com/users/prasadg',
method: 'GET',
responseType: 'stream',
headers: {
'Content-Type': 'application/json',
}
});
);
return new Promise((resolve, reject) => {
writer.on('finish', resolve);
writer.on('error', reject);
});
}
Like I said, the code isn't tested so your gonna have to play with it, but hopefully it puts you in the right direction
Upvotes: 0