无法在admin node.js中进行查询

时间:2017-08-21 16:43:39

标签: javascript node.js firebase firebase-realtime-database google-cloud-functions

我遇到问题,我在JavaScript上运行的代码非常相似,并没有在node.js上运行:

 exports.findDate = functions.https.onRequest((req, res) => {
    console.log("helo: " + req.query.text); // this works

    var ref = admin.database().ref("messages/events");
    const query = ref.orderByChild("start").equalTo(req.query.text);
    query.on("child_added", function(snapshot) {
        console.log(snapshot.val().title);
        res.status(200).send('ok1' + snapshot.toJSON().title);
    });
    res.status(200).send('ok'); //this print in browser
});

奇怪的是,只有查询之外的东西才有效。是什么导致它?

更新代码:

exports.getEvents = functions.https.onRequest((req, res) => {

        admin.database().ref('events').orderByValue().once('value', function(snapshot) {
            console.log(snapshot.val().title);
            res.status(200).send('ok1');
        }).catch(error => {
            console.error('Error while reading data', error);
            res.status(403).send('Error');
        });
    });

enter image description here

2 个答案:

答案 0 :(得分:0)

您应该使用:

<% simple_form_for @user do |f| %>
  <%= f.input :username, input_html: { id: 'user_username', data: { username: @user.username } } %>
  <%= f.input :email, input_html: { id: 'user_email', data: { email: @user.email } } %>
<% end %>

答案 1 :(得分:0)

您无法发回两种状态。由于.once是异步的,因此状态为“正常”。在该工作完成之前返回。如果您检查Firebase控制台,则可能会出现一些错误,表明您尝试发送多个响应。我按原样试用了你的代码并得到了这样的错误: enter image description here

您不需要发送第二个状态响应。如果您想要包含一个catch块,您可以这样做:

query.once('value', function(snapshot) {
    console.log(snapshot.val().title);
    res.status(200).send('ok1');
}).catch(error => {
    console.error('Error while reading data', error);
    res.status(403).send('Error');
});