【问题标题】:How to update request id in real time using cloud firestore in node js?如何在节点 js 中使用 cloud firestore 实时更新请求 ID?
【发布时间】:2018-09-13 21:55:56
【问题描述】:

现在我可以获取 doc id 并将数据插入 DriversCurrentBookings。当新的 currenUserRequest doc id 添加到集合 db 中时,它应该在 firebase 中实时更新(使用 cloud firestore)

这里是使用的代码:

var citiesRef = db.collection('UsersBookingRequest');
var allCities = citiesRef.get()
.then(snapshot => {
    snapshot.forEach(doc => {
        console.log(doc.id, '=>', doc.data());
        var reqID = doc.data().UID;

        var cityRef = db.collection('UsersBookingRequest').doc(reqID);
        var getDoc = cityRef.get()
             .then(doc => {
                 if (!doc.exists) {
                     console.log('No such document!');
                 } else {
                        console.log('Document data:', doc.data());
                        lat = doc.data().Currentlat;
                        long = doc.data().Currentlong;
                        UID = doc.data().UID;
                        // token = doc.data().token;
                        address = doc.data().address;
                        date = doc.data().date;
                        time = doc.data().time;

                        console.log(lat);
                        console.log(long);
                        console.log(UID);
                        // console.log(token);
                        console.log(address);
                        console.log(date);
                        console.log(time);

        var data = {

                 lat: lat,
                 long: long,
                 UID: UID,
                 address: address,
                 date: date,
                 time: time
                 // token: token


                    };

                // Add a new document in collection "cities" with ID 'LA'
        var setDoc = 
db.collection('DriversCurrentBookings').doc(doc.data().UID).set(data);

    }
})
.catch(err => {
    console.log('Error getting document', err);
});

    })
})
.catch(err => {
    console.log('Error getting documents', err);
});

这里是db的截图

【问题讨论】:

    标签: javascript node.js firebase google-cloud-firestore google-api-nodejs-client


    【解决方案1】:

    您可以通过调用其.id.ref(如果您希望将整个引用存储在另一个位置)轻松获取已添加文档的 ID。

    示例 1

    return db.collection('myCollection').add(someData)
      .then(response => {
        console.log(`Document ID: ${response.id}`);
      })
      .catch(err => {
        console.error(err);
      });
    

    示例 2

    let docRef = db.collection('myCollection').doc();
    console.log (`Document ID: ${docRef.id}`);
    
    return docRef.set(someData)
      .then(response => {
        console.log('The data was saved');
      })
      .catch(err => {
        console.error(err);
      });
    

    【讨论】:

    • 改为添加静态数据 db.collection('UsersBookingRequest').doc('9DjekLkfFmTeLfkyZQAeezVriv02');在 doc id 中,我想添加动态 id
    • 是的。我的示例显示添加具有动态 ID 的文档,然后从响应中获取 ID。 .add 将文档添加到集合并创建动态 ID。然后,通过获取响应的 .id 属性来检索它。
    • 我添加了第二个示例,作为替代选项。示例 1 保存文档,然后获取 ID。示例 2 创建一个 ID,然后保存文档
    猜你喜欢
    • 2021-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-14
    • 2016-10-12
    • 1970-01-01
    • 2020-06-10
    • 2019-07-12
    相关资源
    最近更新 更多