Firestore DocumentReference获取字符串形式的路径

时间:2018-11-29 21:59:58

标签: javascript firebase google-cloud-firestore

我在Firestore文档中有一个DocumentReference(在我的示例中为product_option_id),在ExpressJs服务器上,我以JSON形式返回快照,现在我想以String而不是Object的形式获取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”是我的问题

1 个答案:

答案 0 :(得分:0)

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

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

或从数据库中返回适当的数据;)