GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Datastore : only the last entity is saved · Issue #2350 · googleapis/google-cloud-node

Datastore : only the last entity is saved #2350

New issue
Copy link
New issue
Copy link
Closed
Closed
Datastore : only the last entity is saved#2350
Copy link
Assignees
defoishugo
Labels
api: datastoreIssues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.

Description

@defoishugo
defoishugo
opened on Jun 2, 2017
Issue body actions

Hello,

Code :

const Datastore = require('@google-cloud/datastore');

const datastore = Datastore({ projectId: 'lyl-api' });

function addConversationToUser(conversationKey, userKey) {
     conversationKey = datastore.key(['Conversation', conversationKey]);
     var query = datastore.createQuery('Conversation');
     query.hasAncestor(conversationKey);
     return datastore.runQuery(query, function (err, entity) {
          entity = entity[0];
          if (!entity.users) {
               entity.users = [];
          }
          entity.users.push(userKey);
          datastore.save(entity);
          console.log(entity);
     });
}

addConversationToUser(5681777339269120, 'hey');
addConversationToUser(5681777339269120, 'how');
addConversationToUser(5681777339269120, 'are');
addConversationToUser(5681777339269120, 'you');

Console ouput :

{ messages: [], name: 'Kazoo Kid', users: [ 'hey' ] }
{ messages: [], name: 'Kazoo Kid', users: [ 'are' ] }
{ messages: [], name: 'Kazoo Kid', users: [ 'how' ] }
{ messages: [], name: 'Kazoo Kid', users: [ 'you' ] }

Content of the datastore :

 Identifiant
 id=5681777339269120 
 
 Users
 ["are"]

Problem :
Only the last entity is saved.
If I add 'hey' and after that I add 'are', only 'are' is added. If I add after that 'how', only 'how' is added.

Is it a problem from Google ?

Activity

  1. stephenplusplus commented on Jun 2, 2017

    @stephenplusplus
    stephenplusplus
    on Jun 2, 2017
    Contributor
    More actions

    Can you try adding a callback to datastore.save(entity);? These are all asynchronous methods, so you will need to have some type of flow control.

  2. stephenplusplus
    added
    api: datastoreIssues related to the Datastore API.
    type: questionRequest for information or clarification. Not an issue.
    on Jun 2, 2017
  3. defoishugo commented on Jun 2, 2017

    @defoishugo
    defoishugo
    on Jun 2, 2017
    Author
    More actions

    @stephenplusplus

    function addConversationToUser(conversationKey, userKey) {
         conversationKey = datastore.key(['Conversation', conversationKey]);
         var query = datastore.createQuery('Conversation');
         query.hasAncestor(conversationKey);
         return datastore.runQuery(query, function (err, entity, nextQuery) {
              entity = entity[0];
              if (!entity.users) {
                   entity.users = [];
              }
              entity.users.push(userKey);
              datastore.save(entity, function (err, entity) {
                   console.log(entity);
              });
         });
    }
    

    Here is my code, with a callback. Same problem. What do you want to put into the callback ?

  4. stephenplusplus commented on Jun 2, 2017

    @stephenplusplus
    stephenplusplus
    on Jun 2, 2017
    Contributor
    More actions

    The addConversationToUser function itself will need to accept a callback. You might try to use async:

    var async = require('async')
    
    var keyPairs = [
      {
        conversation: '...',
        user: '...'
      },
      // ...
    ]
    
    function addConversationToUser(keyPair, callback) {
      // keyPair = { conversation: '...', user: '...' }
      // ...
      datastore.save(entity, callback)
    } 
    
    async.each(keyPairs, addConversationToUser, function (err) {
      if (err) return console.log(err)
      console.log('successful')
    })
  5. defoishugo commented on Jun 8, 2017

    @defoishugo
    defoishugo
    on Jun 8, 2017
    Author
    More actions

    @stephenplusplus Thanks a lot ! Issue closed.

  6. defoishugo
    closed this as completedon Jun 8, 2017
  7. JustinBeckwith
    assigned
    defoishugo
    on Feb 1, 2021
  8. sofisl
    added 3 commits that reference this issue on Feb 25, 2026

    chore(main): release 7.5.0 (#2350)

    2aa224c

    feat(spanner): A new field `snapshot_timestamp` is added to message `…

    ffa0941

    chore(main): release 8.1.0 (#2305)

    f3bd153
  9. thiyaguk09
    added a commit that references this issue on Mar 18, 2026

    chore(main): release 7.5.0 (googleapis#2350)

    3275f21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @defoishugo
    defoishugo

Labels

api: datastoreIssues related to the Datastore API.type: questionRequest for information or clarification. Not an issue.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402775 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.