尝试导入模块时出现错误。我能做什么?



我试图使用firebase的Firestore数据库与我的小网站,我从https://firebase.google.com/docs/firestore/manage-data/add-data拿了他们的代码。我首先在内置的html脚本中使用配置,但我将其移动到一个文件中。然后我添加了代码,它给了我

SyntaxError: Unexpected token '{'
at /script.js:19:10

下面是我的代码:

Html

<body>
<input type="text" placeholder="type something">
<input type="submit">
<script src="script.js" type="module"></script> <!-- This is where my script is. -->
</body>

Javascript (script.js)

// Import the functions you need from the SDKs you need
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.1.3/firebase-app.js";

const firebaseConfig = { //I don't know if these are sensitive, so i'm censoring them. 
apiKey: "**",
authDomain: "**",
projectId: "**",
storageBucket: "**",
messagingSenderId: "**",
appId: "**"
};
// Initialize Firebase
const app = initializeApp(firebaseConfig);

function writeTest() {
import { doc, setDoc, Timestamp } from "firebase/firestore"; //this is where it says the error is.

const docData = { //this is just test data straight from the site
stringExample: "Hello world!",
booleanExample: true,
numberExample: 3.14159265,
dateExample: Timestamp.fromDate(new Date("December 10, 1815")),
arrayExample: [5, true, "hello"],
nullExample: null,
objectExample: {
a: 5,
b: {
nested: "foo"
}
}
};
await setDoc(doc(db, "data", "one"), docData);
}

这是我如何设置firestore -不同的方式,但它的工作,所以它可能会有所帮助

<编辑>index . html
<script defer src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js"></script>
<script defer src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script>    
<script defer src="/firebase-init.js"></script>
<编辑>firebase-init.js
"use strict";
const firebaseConfig = { 
apiKey: "**",
authDomain: "**",
projectId: "**",
storageBucket: "**",
messagingSenderId: "**",
appId: "**"
};
// Initialize Firebase
firebase.initializeApp(firebaseConfig);
let db = firebase.firestore();
db.collection(collection).doc(docID).get().then(docObj => {})  // read document
db.collection(collection).doc(docId).set(docObj)  // write document to db

相关内容

最新更新