Expected type ‘Firestore$1’, but it was: a custom Firestore object.

huangapple go评论57阅读模式
英文:

Expected type 'Firestore$1', but it was: a custom Firestore object

问题

以下是您要翻译的内容:

"It's my first project in firebase, I'm trying to insert a document (through the emulator), from a Node.js app and I get the error:

Expected type 'Firestore$1', but it was: a custom Firestore object

This is how I initialize firebase:

import admin from 'firebase-admin'
import { connectFirestoreEmulator } from "firebase/firestore";

const firebaseConfig = {
    apiKey: "XXXXX",
    authDomain: "linkedincv-bdbec.firebaseapp.com",
    projectId: "linkedincv-bdbec",
    storageBucket: "linkedincv-bdbec.appspot.com",
    messagingSenderId: "472466845815",
    appId: "XXXXX",
    measurementId: "XXXXX",
};

// Initialize Firebase
const app = admin.initializeApp(firebaseConfig);

const db = admin.firestore();
connectFirestoreEmulator(db, 'localhost', 4000);

export { db }

My execution code is as follows:

import { db } from '../../../lib/middleware/firebaseInit'

export async function POST({ request }) {
    try {
        const payload = await request.json();
        const uniqueUrl = payload.uniqueUrl;

        // const docRef = await setDoc(doc(db, "json_cv", "zied"), payload)
        const docRef = await db.collection("json_cv").doc("zied").set(payload)
        // Return the document ID of the stored object
        return {
            body: JSON.stringify({ documentId: docRef.id }),
            headers: { 'Content-Type': 'application/json' },
            status: 200
        };
    } catch (error) {
        // Handle any errors that occur during processing
        console.error(error);
        return {
            body: JSON.stringify({ error: 'Failed to store unique URL' }),
            headers: { 'Content-Type': 'application/json' },
            status: 500
        };
    }
}

I tried to import connectFirestoreEmulator from other sources but none worked for me. Example:

// import { connectFirestoreEmulator } from 'firebase-admin/firestore';

throws another error"

希望这能帮助您解决问题。如果您需要进一步的帮助,请随时提出。

英文:

It's my first project in firebase, I'm trying to insert a document (through the emulator), from a Node.js app and I get the error:

Expected type 'Firestore$1', but it was: a custom Firestore object

This is how I initialize firebase:

import admin from 'firebase-admin'
import { connectFirestoreEmulator } from "firebase/firestore";

const firebaseConfig = {
	apiKey: "XXXXX",
	authDomain: "linkedincv-bdbec.firebaseapp.com",
	projectId: "linkedincv-bdbec",
	storageBucket: "linkedincv-bdbec.appspot.com",
	messagingSenderId: "472466845815",
	appId: "XXXXX",
	measurementId: "XXXXX",
};

// Initialize Firebase
const app = admin.initializeApp(firebaseConfig);

const db = admin.firestore();
connectFirestoreEmulator(db, 'localhost', 4000);

export {db}

My execution code is as follows:

import {db} from '../../../lib/middleware/firebaseInit'

export async function POST({request}) {
	try {
		const payload = await request.json();
		const uniqueUrl = payload.uniqueUrl;

		// const docRef = await setDoc(doc(db, "json_cv", "zied"), payload)
		const docRef = await db.collection("json_cv").doc("zied").set( payload)
		// Return the document ID of the stored object
		return {
			body: JSON.stringify({ documentId: docRef.id }),
			headers: { 'Content-Type': 'application/json' },
			status: 200
		};
	} catch (error) {
		// Handle any errors that occur during processing
		console.error(error);
		return {
			body: JSON.stringify({ error: 'Failed to store unique URL' }),
			headers: { 'Content-Type': 'application/json' },
			status: 500
		};
	}
}

I tried to import connectFirestoreEmulator from other sources but none worked for me. Example:

// import { connectFirestoreEmulator } from 'firebase-admin/firestore';

throws another error

答案1

得分: 1

Sure, here's the translated code part:

如果你正在运行一个 NodeJS 后端/脚本你必须设置环境变量 `FIRESTORE_EMULATOR_HOST`

const useEmulator = true;

if (useEmulator){
    process.env['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080';
}

admin.initializeApp({ ... });

And the second part:

因此不需要使用这个函数

import { connectFirestoreEmulator } from "firebase/firestore";

You can find the original source here.


<details>
<summary>英文:</summary>

Ok so I found an answer [here][1]

if you are running a NodeJS backend/script, you must set the environement variable `FIRESTORE_EMULATOR_HOST`

    const useEmulator = true;
    
    if (useEmulator){
        process.env[&#39;FIRESTORE_EMULATOR_HOST&#39;] = &#39;localhost:8080&#39;;
    }
    
    admin.initializeApp({ ... });

Thus there&#39;s no need to use the function

    import { connectFirestoreEmulator } from &quot;firebase/firestore&quot;;

  [1]: https://github.com/firebase/firebase-admin-node/issues/776

</details>



huangapple
  • 本文由 发表于 2023年4月13日 16:57:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76003566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定