Functions errors when using firebase firestore

I’m trying to use the firebase admin sdk in one of my netlify functions to update a firebase firestore database. However, when I run the function, it always returns an error.

Here’s the code for my function:

export async function handler(event, context) {
	const id = Math.floor(Math.random() * 10000000 + 1);

	await admin
		.firestore()
		.collection("hello")
		.doc("world")
		.set({ hi: true, id });
	return {
		statusCode: 200,
		body: `done with id ${id}`,
	};
}

Whenever I run it (by opening https://montavistamun.netlify.app/.netlify/functions/test in the browser),
I get an error.

The full functions folder is here.

And the full logs are here:

3:23:17 AM: 2020-08-12T10:23:17.452Z	dfe3d147-ec1a-4193-b527-3f7e31595ffb	ERROR	Invoke Error 	{"errorType":"Error","errorMessage":"error reading first path in the walk /protos\nError: ENOENT: no such file or directory, lstat '/protos'","stack":["Error: error reading first path in the walk /protos","Error: ENOENT: no such file or directory, lstat '/protos'","    at EventEmitter.<anonymous> (/var/task/src/built-netlify-functions/test.js:1135:4027)","    at Object.onceWrapper (events.js:422:26)","    at EventEmitter.emit (events.js:315:20)","    at o (/var/task/src/built-netlify-functions/test.js:1135:2743)","    at O (/var/task/src/built-netlify-functions/test.js:1135:2841)","    at a (/var/task/src/built-netlify-functions/test.js:1135:4093)","    at Function.a.sync (/var/task/src/built-netlify-functions/test.js:1135:4450)","    at Object.<anonymous> (/var/task/src/built-netlify-functions/test.js:375:10125)","    at Object.<anonymous> (/var/task/src/built-netlify-functions/test.js:375:12959)","    at r (/var/task/src/built-netlify-functions/test.js:1:158)"]}
3:23:17 AM: Duration: 276.86 ms	Memory Usage: 91 MB	Init Duration: 250.77 ms	
3:23:36 AM: 2020-08-12T10:23:36.724Z	aabe6a0a-8433-4070-bc2e-c39d3e9b4734	ERROR	Invoke Error 	{"errorType":"TypeError","errorMessage":"t is not a constructor","stack":["TypeError: t is not a constructor","    at /var/task/src/built-netlify-functions/test.js:1135:584","    at new e (/var/task/src/built-netlify-functions/test.js:1135:593)","    at /var/task/src/built-netlify-functions/test.js:856:4457","    at e.ensureService_ (/var/task/src/built-netlify-functions/test.js:856:5922)","    at e.firestore (/var/task/src/built-netlify-functions/test.js:856:4411)","    at e.t (/var/task/src/built-netlify-functions/test.js:840:4869)","    at Runtime.i [as handler] (/var/task/src/built-netlify-functions/test.js:1341:7917)","    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}
3:23:36 AM: Duration: 27.21 ms	Memory Usage: 92 MB	
3:23:47 AM: 2020-08-12T10:23:47.753Z	51e2c2e9-ceda-42b7-8b09-391785978425	ERROR	Invoke Error 	{"errorType":"TypeError","errorMessage":"t is not a constructor","stack":["TypeError: t is not a constructor","    at /var/task/src/built-netlify-functions/test.js:1135:584","    at new e (/var/task/src/built-netlify-functions/test.js:1135:593)","    at /var/task/src/built-netlify-functions/test.js:856:4457","    at e.ensureService_ (/var/task/src/built-netlify-functions/test.js:856:5922)","    at e.firestore (/var/task/src/built-netlify-functions/test.js:856:4411)","    at e.t (/var/task/src/built-netlify-functions/test.js:840:4869)","    at Runtime.i [as handler] (/var/task/src/built-netlify-functions/test.js:1341:7917)","    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}
3:23:47 AM: Duration: 6.29 ms	Memory Usage: 93 MB	
3:29:44 AM: 2020-08-12T10:29:44.570Z	4c4a1cf9-c949-4d1b-8637-63063da8cef3	ERROR	Invoke Error 	{"errorType":"TypeError","errorMessage":"t is not a constructor","stack":["TypeError: t is not a constructor","    at /var/task/src/built-netlify-functions/test.js:1135:584","    at new e (/var/task/src/built-netlify-functions/test.js:1135:593)","    at /var/task/src/built-netlify-functions/test.js:856:4457","    at e.ensureService_ (/var/task/src/built-netlify-functions/test.js:856:5922)","    at e.firestore (/var/task/src/built-netlify-functions/test.js:856:4411)","    at e.t (/var/task/src/built-netlify-functions/test.js:840:4869)","    at Runtime.i [as handler] (/var/task/src/built-netlify-functions/test.js:1341:7917)","    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"]}
3:29:44 AM: Duration: 2.02 ms	Memory Usage: 93 MB

I found a somewhat related issue here, but the solution from that issue doesn’t work, and my function builds properly.

Hey @jeffkmeng,
I just checked this function endpoint and your site’s “Functions” tab, and test.js seems to have disappeared! Maybe you’ve resolved this since writing in initially? If not, let us know the new function name/location and we’ll keep digging.

Hi @jen,

Yes, I’ve resolved this issue by switching to the firebase rest api. Thanks.

1 Like

Hi can you please share how you dis your solution?
I have great struggle getting the Admin sdk to work with Netlfy Lambda

Hi @Mik-A, I responded to your other post about this. But to answer your question, I think @jeffkmeng stopped using firebase-admin and is using the firebase rest api directly to accomplish what they needed.

Hi @Mik-A,

The firebase admin SDK doesn’t seem to work with netlify, so I ended up just using the rest api.
I wrote some helper functions here for basic data retrieval and updates.

Here’s an example of how I used it:

const database = require("./path/to/fbUtils");

// get data -- this directly returns the data as an object
const data = await database.get("/collection/doc");

// nested documents work too
const moreData = await database.get("/collection/doc/childcollection/anotherdoc");
// update some data
await database.update("/collection/doc/", {
	myData: 23,
	timestamp: database.currentTimestamp()
})
// add some data
await database.add("/collection/", {
	myData: 23,
	timestamp: database.currentTimestamp()
})
2 Likes

is the service account (in fbUtils) the whole service account object or just one (which)?