This repository was archived by the owner on Mar 10, 2020. It is now read-only.
This repository was archived by the owner on Mar 10, 2020. It is now read-only.
Error when adding file: "TypeError: First argument must be a string or Buffer" #687
Closed
Description
When calling the add function, I'm getting the following error:
TypeError: First argument must be a string or Buffer
My service class (Angular/Typescript):
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Subscriber } from 'rxjs/Subscriber';
import { HttpClient } from '@angular/common/http';
import * as ipfsApi from 'ipfs-api';
import { AddResult } from '../models/AddResult';
import { readFileSync } from 'fs';
@Injectable()
export class IpfsService {
ipfs: any;
constructor(public _http: HttpClient) {
this.ipfs = ipfsApi('localhost', '8080', { protocol: 'http' });
}
add(file: File, progressFunction?: Function): Observable<AddResult> {
return new Observable<AddResult>((subscriber: Subscriber<AddResult>) => {
const files = [
{
path: file.path,
content: readFileSync(file.path)
}
];
const options = {
progress: (bytes: number) => {
if (progressFunction) {
progressFunction(bytes)
}
}
};
this.ipfs.files.add(files, options, (err, res) => {
if (err) {
subscriber.error(err);
} else {
for (let i = 0; i < res.length; i++) {
subscriber.next(res[i]);
}
subscriber.complete();
}
});
});
}
}
When checking whether readFileSync(file.path)
returns a buffer by calling Buffer.isBuffer(readFileSync(file.path))
, false is returned.
Environment:
Angular: 5.2.0
Electron: 1.7.11
Node: 8.9.3 (Windows 10)