简体   繁体   English

无法从Node.js从Google数据存储获取数据

[英]Can't get data from Google Datastore from Node.js

I can't get data from the method getuserpost . 我无法从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. 其余代码可以很好地添加和删除数据,然后重定向到主页,但该方法不会显示JSON输出。 I have tried with res.send and res.render but nothing. 我尝试使用res.sendres.render但是什么也没有。

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: 这是该方法的HTML:

<!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. 我正在将Google数据存储区与Google App Engine一起使用。

我猜您错过了筛选器中的运算符选项。

.filter('girl', "=", req.body.girl_field)
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. 问题在于getuserpost中的代码,当我注释代码时,它可以工作,但是当我删除res.send(“ output”)并取消注释时,代码就卡住了。 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('/');
            }
        });
    });
});

SOLUTION FOR READING DATA FROM "DATASTORE" IN "APP ENGINE" USING "NODE JS": 使用“ NODE JS”从“ APP引擎”中的“数据存储”中读取数据的解决方案:

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);
            }
        });
    });
});

SOLUTION FOR ADDING DATA FROM "DATASTORE" IN "APP ENGINE" USING "NODE JS": 使用“ NODE JS”在“ APP ENGINE”中从“ DATASTORE”添加数据的解决方案:

//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('/');
    });

});

SOLUTION FOR UPDATING DATA FROM "DATASTORE" IN "APP ENGINE" USING "NODE JS": 使用“ NODE JS”从“ APP ENGINE”中的“ DATASTORE”更新数据的解决方案:

//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('/');
          });
    });
});

You need composite indexing for the fields girl and timestamp . 您需要组合索引girltimestamp Docs on composite indexes here . 有关综合索引的文档,请参见此处

Your yaml file should contain the following 您的yaml文件应包含以下内容

- 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM