How to set your key for GAE Datastore?

一世执手 提交于 2019-12-12 04:34:13

问题


Anyone have an idea why I keep getting this error when I make a GET request to /display-message? { Error: Key path element must not be incomplete: [created_timestamp: ]. Am I not setting my key the right way? You can view the backend here https://jarvis-hd-live-151521.appspot-preview.com/display-message?last_updated=0

const key = datastore.key('created_timestamp', 5689413791121408);

app.post('/message', function (request, response) {
  let message = request.body.Body;
  response.send("<Response><Message>Heyyo!</Message></Response>");

  var d = new Date;
  var time = d.getTime();

  let entity = {
      key: key,
      data: {
        message: message,
        created: time
      }
  };


  datastore.upsert(entity)
    .then(() => {
        console.log("Post UPDATED successfully");
    });



});


app.get('/display-message', function(req,res){

    let last_updated_time = req.query.last_updated;     


    function updateMessage(timestamp, message) {
        var output = {
            success: 1,
            data: message,
            timestamp: timestamp
        };

        return res.json(output);
    }

    function checkMessage() {
        console.log("Starting to check message function");
        var timeout;
        clearTimeout(timeout);

        datastore.get(key)
            .then((entities) => {
                let entity_data = entities[0];
                console.log(entity_data);

                let message_text = entity_data.message;
                let new_timestamp_value = entity_data.created;

                if(!last_updated_time || last_updated_time < new_timestamp_value) {
                    updateMessage(new_timestamp_value, message_text);
                }
                else {
                    console.log("No new messages at this time");

                    timeout = setTimeout(function(){
                        checkMessage();
                    }, 1000 * 10); //10 seconds
                }

            })
            .catch(function(){
                console.log("Promise Rejected");
                console.log(err);
            })
    };

    checkMessage();

});

回答1:


I believe datastore.key() is expecting a list:

const key = datastore.key(['created_timestamp', 5689413791121408]);

https://googlecloudplatform.github.io/google-cloud-node/#/docs/datastore/0.6.0/datastore?method=key



来源:https://stackoverflow.com/questions/41307647/how-to-set-your-key-for-gae-datastore

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!