0

I can't get data from the method getuserpost. The web page stucks and doesn't show anything.

The rest of the code works well adding and deleting data and then redirecting to the home page but that method does not show the JSON output. I have tried with res.send and res.render but nothing.

Does someone know what's wrong with this code?

app.get('/getuser', (req, res) => {
    res.render('getuser');
});
//DOESN'T WORK
app.post('/getuserpost', (req, res) => {
    const query = datastore.createQuery('usersTable').filter('girl', req.body.girl_field).order('timestamp', { descending: true }).limit(10);
    datastore.runQuery(query).then((results) => {
        const entities = results[0];
        res.json(entities[0]);
    });
});

This is the HTML for that method:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <form action="/getuserpost/" method="POST">     
        <label for="girl_name">Enter girl: </label>
        <input id="girl_name" type="text" name="girl_field" value="Default girl for user">
        <input type="submit" value="OK">
    </form>
    <br>
    <a href="/home">Home</a>
    <br>
    <a href="/adduser">Add user</a>
    <br>
    <a href="/updateuser">Update user</a>
    <br>
    <a href="/deleteuser">Delete user</a>
    <br>
</body>

</html>

I'm using Google Datastore with Google App Engine.

14
  • Please specify error message or execution results, Commented Jan 16, 2018 at 15:26
  • I don't know how to see error logs in app engine Commented Jan 16, 2018 at 19:44
  • Go to logging and select your deployed app. Also, you can check network tab in the browser, to see what has been returned by the server. Commented Jan 16, 2018 at 19:45
  • When I press ok, it doen't do nothing. The wheel of charge in the expolorer is running and running without doing nothing Commented Jan 16, 2018 at 19:55
  • "Error: Unsupported field value, undefined, was provided" I don't understand because I can add and delete but not read Commented Jan 16, 2018 at 20:09

6 Answers 6

1

I guess you missed operator option in filter.

.filter('girl', "=", req.body.girl_field)
Sign up to request clarification or add additional context in comments.

3 Comments

I have tried that, but doesn't work. The delete method works and doesn't use that "=" sign. I have deleted all old error messages in App Engine I have used the get user web several times but no new errors arise. I have used "gcloud app deploy index.yaml" before using "gcloud app deploy" and neither. I have seen the "Network" tab in the explorer and nothing is showed.
When I use Postman I receive this: <html> <head> <title>502 Bad Gateway</title> </head> <body bgcolor="white"> <center> <h1>502 Bad Gateway</h1> </center> <hr> <center>nginx</center> </body> </html>
operator is optional in case of =
1

SOLUTION FOR READING DATA FROM "DATASTORE" IN "APP ENGINE" USING "NODE JS":

app.get('/getuser', (req, res) => {
res.render('getuser');
});
//WORKS
app.post('/getuserpost', (req, res) => {
    const query = datastore.createQuery('usersData').filter('girl', 
req.body.girl_field);
    datastore.runQuery(query).then((results) => {
        const entities = results[0];
        datastore.get(entities[0][datastore.KEY], (err, entity) => {
            if (!err) {
                res.send(entity);
            }
        });
    });
});

2 Comments

You are doing something wrong. Entities already should contains data. We've using such approach every where in our project. Did you logged what is inside entities?
You are right, the code works as well without the datastore.get() method. The problem was all time the "const query" variable. I got rid of the methods after "filter" in the query and the code worked perfect. Thanks
0
app.get('/getuser', (req, res) => {
res.render('getuser');
});
//WORKS
app.post('/getuserpost', (req, res) => {
    /*const query = datastore.createQuery('usersData').filter('girl', 
req.body.girl_field).order('timestamp', { descending: true }).limit(10);
    datastore.runQuery(query).then((results) => {
       const entities = results[0];
        datastore.get(entities[0][datastore.KEY], (err, entity)=>{
            if(!err){
                res.json(entity);
            }else{
                console.log(err);
            }
        });
    });*/
    res.send("output");

});

The problem is with the code inside getuserpost, when I comment the code It works but when I delete res.send("output") and uncomment the code It stucks. This commented version doesn't work neither. This delete method below works and its query works

app.get('/deleteuser', (req, res) => {
    res.render('deleteuser');
});
//WORKS
app.post('/deleteuserpost', (req, res) => {
    const query = datastore.createQuery('usersData').filter('car', req.body.car_field);
    datastore.runQuery(query).then((results) => {
        const entities = results[0];
        datastore.delete(entities[0][datastore.KEY], (err) => {
            if (!err) {
                res.redirect('/');
            }
        });
    });
});

Comments

0

SOLUTION FOR ADDING DATA FROM "DATASTORE" IN "APP ENGINE" USING "NODE JS":

//WORKS
app.post('/adduserpost', (req, res) => {

    const user = {
        timestamp: new Date,
        name: req.body.name_field,
        girl: req.body.girl_field,
        car: req.body.car_field
    }

    datastore.save({
        key: datastore.key('usersData'),
        data: user
    }).then(() => {
        res.redirect('/');
    }).catch((err) => {
        res.redirect('/');
    });

});

1 Comment

This is unbelievable. I am writing CRUD methods that work for me to help other people who see this in the future and I am penalized. I quit
0

SOLUTION FOR UPDATING DATA FROM "DATASTORE" IN "APP ENGINE" USING "NODE JS":

//WORKS
app.post('/updateuserpost', (req, res) => {
    const query = datastore.createQuery('usersData').filter('car', req.body.car_field);
    datastore.runQuery(query).then((results) => {
        let entities = results[0];
        entities[0].car = req.body.new_car_field;
        const entity = {
            key: entities[0][datastore.KEY],
            data: entities[0],
          };

          datastore.update(entity).then(() => {
            res.redirect('/');
          });
    });
});

Comments

0

You need composite indexing for the fields girl and timestamp. Docs on composite indexes here.

Your yaml file should contain the following

- kind: usersTable
  properties:
  - name: girl
  - name: timestamp
    direction: desc

To apply the composite indexes, run the following command

gcloud -q datastore create-indexes path/to/yaml/file

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.