我目前正在尝试在我的(Firebase)nodejs 项目中导入和使用 nanoid。我安装了它
npm i nanoid
我试着用
import { nanoid } from 'nanoid'
和
import { nanoid } from '../node_modules/nanoid/nanoid.js'
我尝试的一切都失败了。我是 nodejs 和 js 本身的完整初学者,但没有网站或文档帮助我解决问题。我只想要一个唯一的 id :(
这是我的 index.html (减少到最低限度:
<!DOCTYPE html>
<html>
<head>
<title>Welcome to Firebase Hosting</title>
!!!Here are Firebase imports!!!
<script defer src="/__/firebase/init.js?useEmulator=true"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="registerContainer">
<div class="registerContent">
<h1 id="title">Sign up</h1>
<iframe name="hiddenFrame" width="0" height="0" border="0" style="display: none;"></iframe>
<form id="form" onsubmit="return false">
<!-- Birth date Input (required) -->
<div class="input_field">
<input id="date" onfocus="(this.type = 'date')" class="text_input" type="text" name="birthdate" placeholder="Geburtsdatum" required />
</div>
<!-- Checkbox 1 -->
<div class="input_field checkbox_option">
<input type="checkbox" id="cb1" class="checkbox">
<label for="cb1">I agree with terms and conditions</label>
</div>
<input class="button" id="registerUser" type="submit" value="Anmelden"/>
</form>
</div>
</div>
<script src="app.js"></script>
</body>
</html>
应用程序.js:
const nanoid = require('nanoid');
document.addEventListener('DOMContentLoaded', event => {
const app = firebase.app();
const db = firebase.firestore();
const users = db.collection('users');
})
async function addUser() {
console.log('adding User');
const db = firebase.firestore();
const users = db.collection('users');
const data = {
email: document.getElementById("email").value,
test_active: false
};
code = nanoid(10).toString();
await users.doc(code).set(data);
}