smalldata-industries / starling-api

RESTful API for Starling, a decentralized storage application powered by Filecoin.
MIT License
16 stars 1 forks source link

TypeError: Cannot read property 'toString' of undefined #4

Open shc261392 opened 4 years ago

shc261392 commented 4 years ago

Issue description

I encountered this error message when calling the store API:

{"message":"TypeError: Cannot read property 'toString' of undefined\n    at Object.proposeStorageDeal (/Users/james/numbers/starling-api/node_modules/filecoin-api-client/src/  cmd/client/propose-storage-deal.js:10:18)\n    at proposeDeal (/Users/james/numbers/starling-api/lib/apis/store/controller/store.js:115:51)\n    at ProposeDeals.Promise.all.   importedFiles.map.file (/Users/james/numbers/starling-api/lib/apis/store/controller/store.js:157:16)\n    at Array.map (<anonymous>)\n    at ProposeDeals (/Users/james/   numbers/starling-api/lib/apis/store/controller/store.js:154:21)\n    at store (/Users/james/numbers/starling-api/lib/apis/store/index.js:29:11)\n    at process._tickCallback   (internal/process/next_tick.js:68:7)","level":"error"}

After adding some debug console to the starling-api, I've found that the cid and name parameter passed to this function is always undefined if only 1 file is going to be stored.

It turns out that in this line

const FILE = filesCount === 1 ? file : file[0];

return proposeDeal(db, FILE, miners, i);

The FILE object is actually an array if there's only one file (the filesCount here is 1)

[ { cid: 'Qmf2WJRKky6WNnsmUs9pnQ39zufebJ6xusHYfbPS99yXjX',
fileSize: 13409,
name: 'xmp.txt' } ]

but the function proposeDeal should accept the file object instead of an array of the file objects

Steps to reproduce

The complete Python 3 code to invoke the API:

#!/usr/bin/env python3
 import json

 import requests

 base_url = 'http://localhost:3000'

 base_headers = {
     'Accept': 'application/vnd.starling+json; version=1.0',
 }

 def store():
     with open('xmp.json', 'r') as f:
         data = json.load(f)

     res = requests.post(
         url=base_url + '/api/store',
         headers=base_headers,
         data=data,
     )
     try:
         res.raise_for_status()
     except Exception as e:
         print(e.response)
     return res.text

 r = store()
 print(r)

xmp.json:

{
     "dataPath": "/Users/james/numbers/starling-test/xmp/"
 }

where the xmp/ folder contains only 1 file, an xmp.txt file

Environment

starling-api commit hash: d6cb6b435e2ce5cf11d2d647e18d56db9329be9f filecoin version: v0.5.8

mishmosh commented 4 years ago

@finoradin Just to confirm, this bug with ProposeDeal is at the starling-api level and not go-filecoin? (cc @ognots)

finoradin commented 4 years ago

@finoradin Just to confirm, this bug with ProposeDeal is at the starling-api level and not go-filecoin? (cc @ognots)

I haven't tried to reproduce it, but based on how it has been reporting, yes that is my understanding.

bafu commented 4 years ago

@mishmosh @finoradin Thanks for the comments. According to current status, do you have any suggestion about using the store API?

mishmosh commented 4 years ago

This issue is expected to be fixed early next week, with the rewrite of the store command as part of the migration to lotus that @alexandrumatei36 is working on. I will update here once that's merged in.