ElyaConrad / iCloud-API

Node API for Apple's iCloud services
1.18k stars 98 forks source link

"Cannot read property 'map' of undefined" in Notes.js after unhandled server error #51

Open zudljk opened 4 years ago

zudljk commented 4 years ago

When a request to resolve a note fails on the server side (e.g. 400 Bad Request), a JSON will be returned that doesn't a "results" field.

This was the payload returned from the server from at least one of the notes in my iCloud account:

{
  "uuid" : "2487d368-fc70-410d-8c0b-ec957399b58a",
  "serverErrorCode" : "BAD_REQUEST",
  "reason" : "BadRequestException: missing required field 'value'"
}

Therefore, this line will produce a TypeError since result.results will be undefined:

const records = result.results.map(res => res.rootRecord).map(record => {

Exception stack trace:

TypeError: Cannot read property 'map' of undefined
    at Request._callback (y:\src\icloudapi-test\node_modules\apple-icloud\resources\apps\Notes.js:139:40)
    at Request.self.callback (y:\src\icloudapi-test\node_modules\request\request.js:185:22)
    at Request.emit (events.js:182:13)
    at Request.<anonymous> (y:\src\icloudapi-test\node_modules\request\request.js:1154:10)
    at Request.emit (events.js:182:13)
    at IncomingMessage.<anonymous> (y:\src\icloudapi-test\node_modules\request\request.js:1076:12)
    at Object.onceWrapper (events.js:275:13)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1091:14)
    at process._tickCallback (internal/process/next_tick.js:174:19)

The reason was that at least one of my notes fetched by myCloud.Notes.fetch() had no "shortGUID" field. Filtering out this note fixed the error for me, but there should be a general handling of Bad Request errors.

Apart from that, thanks for the great work on this project!

JOJ0 commented 4 years ago

Hi, I am experiencing the same error, and thus trying to figure out where and what the shortGUID field is but it seems I can't find it. Trying stuff like this:

const notes = myCloud.Notes.fetch();
notes.on("data", async zone => {
       ....
       .... 
        console.log(myNote.fields.Note.value['recordName'])
        console.log(myNote.fields.Note.value)
        console.log(myNote)
}

Maybe I am on the wrong track but would like to find notes where this field is missing and filter them out as suggested. Where inside "myNote" should this field be located?

Thanks!

JOJ0 commented 4 years ago

Hi @zudljk sorry for bothering you but this is proably an easy one for you ^

I would like to use the same workaround as you did but can't locate the shortGUID anywhere in my objects. It drive's me kind of crazy - and yes I am not too familiar with javascript - coming from python. Well anyway, maybe you could give me some heads up. Thanks in advance!

Jojo

zudljk commented 4 years ago

Hi @JOJ0, sorry for the late answer, as far as I remember, the shortGUID property is directly below the note object itself. At least that's where I check for it in my own project (https://github.com/zudljk/inotes2md/blob/master/inotes2md.js)

JOJ0 commented 4 years ago

Yes! this is exactely the answer I was looking for! I am sure it will help. I will report my findings here, it might help others too. Will take some time until I get to work on this again. Thanks a lot! :-)