cloud-nodejs icon indicating copy to clipboard operation
cloud-nodejs copied to clipboard

Error: Key is not defined

Open arjunpola opened this issue 8 years ago • 4 comments

After creating the first Entity it and try to run the node app according to STEP-6 I get the following error.

Running on http://localhost:8080/
/home/user_name/cloud-nodejs/start/app.js:40
    var keyBooks = books.map((book) => Object.assign(book, { id: book.id || book[key].path[1] }));
                                                                                 ^
ReferenceError: key is not defined
    at books.map (/home/user_name/cloud-nodejs/start/app.js:40:82)
    at Array.map (native)
    at /home/user_name/cloud-nodejs/start/app.js:40:26
    at /home/user_name/cloud-nodejs/start/node_modules/google-cloud/node_modules/@google-cloud/datastore/src/request.js:577:7
.....

Solution:

I works when I change from id = 0 to id = 1 for the entity I created.

arjunpola avatar Mar 30 '17 21:03 arjunpola

I had the same error and indeed it worked for me too! So the entry created, Chalres Dickens's book in the Datastore should be created with id = 1

The real problem is that when you create the Book table in the Datastore you should not create a id row! If you did delete or at each new entry (book) enter a id of 1 for instance. For that you need to modify the addBook function in book.js and the Addbook call in app.js

tonoli avatar Jun 13 '17 01:06 tonoli

Changing the value of id from 0 to 1 didn't work for me unfortunately...

I am getting the below error...

raj_dhandus@my-nodejs-codelab-1217-190421:~/cloud-nodejs/start$ node app.js
Running on http://localhost:8080/
/home/raj_dhandus/cloud-nodejs/start/app.js:40
    var keyBooks = books.map((book) => Object.assign(book, { id: book.id || book[key].path[1] }));
                                                                                 ^

ReferenceError: key is not defined
    at books.map (/home/raj_dhandus/cloud-nodejs/start/app.js:40:82)
    at Array.map (<anonymous>)
    at /home/raj_dhandus/cloud-nodejs/start/app.js:40:26
    at /home/raj_dhandus/cloud-nodejs/start/node_modules/google-cloud/node_modules/@google-cloud/datastore/src/request.js:577:7
    at ConcatStream.<anonymous> (/home/raj_dhandus/cloud-nodejs/start/node_modules/google-cloud/node_modules/concat-stream/index.js:36:43)
    at emitNone (events.js:110:20)
    at ConcatStream.emit (events.js:207:7)
    at finishMaybe (/home/raj_dhandus/cloud-nodejs/start/node_modules/google-cloud/node_modules/readable-stream/lib/_stream_writable.js:513:14)
    at endWritable (/home/raj_dhandus/cloud-nodejs/start/node_modules/google-cloud/node_modules/readable-stream/lib/_stream_writable.js:523:3)
    at ConcatStream.Writable.end (/home/raj_dhandus/cloud-nodejs/start/node_modules/google-cloud/node_modules/readable-stream/lib/_stream_writable.js:493:41)

Update: As soon as i created one more entity it started working.. had the same data in both the entities (rows with 1 as the id). Still not clear what the issues was..

rajdhandus avatar Dec 28 '17 21:12 rajdhandus

What about step 7?!

morwin10 avatar Jan 14 '18 00:01 morwin10

If there is no id property then the second part of the expression comes book[key].path[1], however it is not a simple key in the array now but Symbol(KEY). It is the books array in books.getAllBooks callback:

[ { imageUrl: '',
    publishedDate: '',
    createdBy: '',
    title: 'A Tale of Two Cities',
    description: '',
    author: 'Charles Dickens',
    [Symbol(KEY)]:
     Key {
       namespace: undefined,
       id: 5629499534213120,
       kind: 'Book',
       path: [Getter] } } ]

I added the gcloud and datastore to the app.js too, and changed book[key].path[1] to book[datastore.KEY].path[1], and works without additional id property.

matexus avatar Feb 12 '18 20:02 matexus