英文:
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['FIRESTORE_EMULATOR_HOST'] = 'localhost:8080';
}
admin.initializeApp({ ... });
Thus there's no need to use the function
import { connectFirestoreEmulator } from "firebase/firestore";
[1]: https://github.com/firebase/firebase-admin-node/issues/776
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论