Saturday, 4 January 2020

Cloud functions throws error after migrating Angular Universal firebase

I have a SPA which is hosted in Firebase and I have been using Firestore to store the data. I am also making use of cloud functions for few of my https operations and certain other database read and write.

Recently I updated my rendering logic from client side to server side with angular universal which is pretty successful. Here's the link I followed: https://fireship.io/lessons/angular-universal-firebase/

Basically, I had created a https function to render ssr in cloud functions.

const universal = require(`${process.cwd()}/dist/server`).app;
exports.api = functions.https.onRequest(app); //Application related endpoint line makepayment, createorder etc.,
exports.ssr = functions.https.onRequest(universal);

Now, once I deploy this function, all the api https function, where I used to access db.collection or db.doc started throwing error. Below is the same call for db.doc.

db.doc("samplecollection/docid")
.get()
.then(function (doc) {
   console.log('doc.data', doc.data());
})
.catch(function (error) {
   console.log('Error in fetching samplecollection doc', error);
});

Now when I try to do above I get the below error.

Error in autogenerated TypeError [ERR_INVALID_ARG_TYPE]: The "path"
 argument must be of type string. Received type object
      at validateString (internal/validators.js:125:11)
      at Object.basename (path.js:744:5)
      at GrpcClient.loadProto (sampleproject\functions\node_modules\google-gax\build\src\grpc.js:133:29)
      at new FirestoreClient (sampleproject\functions\node_modules\@google-cloud\firestore\build\src\v1\firestore_client.js:121:32)
      at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory]
 (sampleproject\functions\node_modules\@google-cloud\firestore\build\src\index.js:302:26)
      at ClientPool.acquire (sampleproject\functions\node_modules\@google-cloud\firestore\build\src\pool.js:67:35)
      at ClientPool.run (sampleproject\functions\node_modules\@google-cloud\firestore\build\src\pool.js:124:29)
      at Firestore.readStream (sampleproject\functions\node_modules\@google-cloud\firestore\build\src\index.js:947:26)
      at Firestore.getAll_ (sampleproject\functions\node_modules\@google-cloud\firestore\build\src\index.js:680:14)
      at initializeIfNeeded.then (sampleproject\functions\node_modules\@google-cloud\firestore\build\src\index.js:650:61)
      at ZoneDelegate.invoke (sampleproject\functions\dist\server.js:5715:30)
      at Zone.run (sampleproject\functions\dist\server.js:5472:47)
      at sampleproject\functions\dist\server.js:6213:38
      at ZoneDelegate.invokeTask (sampleproject\functions\dist\server.js:5750:35)
      at Zone.runTask (sampleproject\functions\dist\server.js:5517:51)
      at drainMicroTaskQueue (sampleproject\functions\dist\server.js:5930:39)
      at process._tickCallback (internal/process/next_tick.js:68:7)

I am not really sure why the error says The "path" argument must be of type string. Received type object. I tried to assign path of document to a variable and check its type with typeof, it still says as string.

Key point to note is - if I remove ssr, function and dist folder copied into functions directory, everything with same line of code works properly. So, I strongly suspect, this has something to do with SSR.

I made a lot of google search for this but none of them had this set-up of SSR. Could someone please point me in right direction or let me know if anyone has faced this issue with Angular Universal SSR and found a solution?


Update - 1

I have below option set in webpack.server.config.js file. Will that be a problem?

node: {
    __dirname: false
},


from Cloud functions throws error after migrating Angular Universal firebase

No comments:

Post a Comment