【问题标题】:Firestore DocumentReference get path as stringFirestore DocumentReference 以字符串形式获取路径
【发布时间】:2018-11-29 21:59:58
【问题描述】:

我在 Firestore 文档中有一个 DocumentReference(在我的示例中为 product_option_id),在我的 ExpressJs 服务器上,我将快照作为 JSON 返回,现在我想将 DocumentPath 作为字符串而不是对象获取。

这是我的代码@Firebase Cloud Functions:

const app = express();
app.set('view engine', 'pug');
app.get('/api/example.json', function (req, res) {
  db.collection("products").doc('liv-22956').get()
    .then(function(querySnapshot) {
      res.json(querySnapshot.data());
    });
});

我得到的结果:

{"vat":null,"product_option_id":{"_firestore":{"_firestoreClient":{"auth":{"authClientPromise":{},"authClient":{"domain":null,"_events":{},"_eventsCount":0,"transporter":{},"credentials":{"access_token":"ya***","scope":"https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/firebase https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/cloudplatformprojects.readonly","token_type":"Bearer","id_token":"eyJhbGciONj*********g","expiry_date":1543531208403,"refresh_token":"1/K****"},"certificateCache":null,"certificateExpiry":null,"refreshTokenPromises":{},"_clientId":"563584335869-fgrhgmd47bqnekij5i8b5pr03ho849e6.apps.googleusercontent.com","_clientSecret":"***","eagerRefreshThresholdMillis":300000,"_refreshToken":"1/Kj***","scopes":["https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/datastore"]},"googleAuthClient":{"jsonContent":{"client_id":"563584335869-fgrhgmd47bqnekij5i8b5pr03ho849e6.apps.googleusercontent.com","client_secret":"******i","type":"authorized_user","refresh_token":"1/K******L"},"cachedCredential":{"domain":null,"_events":{},"_eventsCount":0,"transporter":{},"credentials":{"access_token":"y*****M","scope":"https://www.googleapis.com/auth/cloud-platform https://www.googleapis.com/auth/firebase https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/cloudplatformprojects.readonly","token_type":"Bearer","id_token":"eyJhb*******g","expiry_date":1541234568403,"refresh_token":"1/KjaNt******L"},"certificateCache":null,"certificateExpiry":null,"refreshTokenPromises":{},"_clientId":"5******e6.apps.googleusercontent.com","_clientSecret":"<<<<*******V0sAi","eagerRefreshThresholdMillis":300000,"_refreshToken":"1/Kja****L","scopes":["https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/datastore"]},"_cachedProjectId":"**********","_getDefaultProjectIdPromise":{}},"config":{"scopes":["https://www.googleapis.com/auth/cloud-platform","https://www.googleapis.com/auth/datastore"],"projectId":"******u","libName":"gccl","libVersion":"0.14.1"},"credentials":null,"environment":{},"jwtClient":null,"projectId":"********-"}},"_initalizationOptions":{"projectId":"*********-lu","libName":"gccl","libVersion":"0.14.1"},"_clientInitialized":{},"_preferTransactions":false,"_lastSuccessfulRequest":1543527608665,"_referencePath":{"segments":[],"_formattedName":"projects/**************-lu/databases/(default)","_projectId":"**fsd"*******u","_databaseId":"(default)"}},"_referencePath":{"segments":["abcd","reference"],"_projectId":"************","_databaseId":"(default)"}},"position":null,"purchase_price":null,"name":"******","price_m":"**************","id":*********,"product_category_id":"**********","created_at":"1234","stock":true,"hidden":false,"modal":null,"brand_id":null,"product_option_modal_id":"","description":""}

我想要什么:

{"vat":null,"product_option_id":"abcd/reference","position":null,"purchase_price":null,"name":"******","price_m":"**************","id":*********,"product_category_id":"**********","created_at":"1234","stock":true,"hidden":false,"modal":null,"brand_id":null,"product_option_modal_id":"","description":""}

“product_option_id”键是我的问题

【问题讨论】:

    标签: javascript firebase google-cloud-firestore


    【解决方案1】:

    所以你应该做这样的事情:

    const app = express();
    app.set('view engine', 'pug');
    app.get('/api/example.json', function (req, res) {
      db.collection("products").doc('liv-22956').get()
        .then(function(querySnapshot) {
          const data = querySnapshot.data();
          res.json({ 
            vat: data.null,
            product_option_id: data._referencePath.segments[0] + data._referencePath.segments[0]
            ...
          });
        });
    });
    

    或者从你的数据库中返回正确的数据;)

    【讨论】:

    • 感谢您的回答,问题是,如何从我的数据库中返回正确的数据 ;) ...我知道我可以这样做 data._referencePath。 segment.forEach(...) 但我认为这不是最好的方法? ://
    猜你喜欢
    • 1970-01-01
    • 2018-11-16
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多