Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: Key is not defined #17

Open
arjunpola opened this issue Mar 30, 2017 · 4 comments
Open

Error: Key is not defined #17

arjunpola opened this issue Mar 30, 2017 · 4 comments

Comments

@arjunpola
Copy link

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.

@tonoli
Copy link

tonoli commented Jun 13, 2017

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

@rajdhandus
Copy link

rajdhandus commented Dec 28, 2017

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..

@morwin10
Copy link

morwin10 commented Jan 14, 2018

What about step 7?!

@matexus
Copy link

matexus commented Feb 12, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants