This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Use this file in your project. | |
But first, create a Node project: | |
npm init -y | |
Then add a line to package.json: | |
"type": "module", | |
Usage: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add your secrets here | |
CONNECTION_STRING=yes-here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Schema, model, connect, Mongoose } from 'mongoose' | |
// Animal is an interface describing animal objects | |
import { Animal } from './animal.js'; | |
const animalSchema = new Schema<WithId<Animal>>({ | |
_id: ObjectId, // from WithId | |
species: String, // from Animal | |
factoid: String, // from Animal | |
score: Number // from Animal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This example produced 2024-05-13 | |
// vite.config.js | |
import { defineConfig } from 'vite' | |
import react from '@vitejs/plugin-react-swc' | |
// https://vitejs.dev/config/ | |
export default defineConfig({ | |
plugins: [react()], | |
server: { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import the functions you need from the SDKs you need | |
import { initializeApp } from "firebase/app"; | |
import { getFirestore, collection, addDoc, getDocs, getDoc, doc, updateDoc, deleteDoc } from 'firebase/firestore/lite' | |
// Get this from Firebase Console | |
import { firebaseConfig } from "./firebaseConfig.js"; | |
// Initialize Firebase and get database | |
const app = initializeApp(firebaseConfig); | |
const db = getFirestore(app) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
The Budget component uses "conditional rendering" to do | |
different things based on its props. What will each line | |
in the App component render? | |
*/ | |
const App = () => ( | |
<main> | |
<Budget value={5500} /> | |
<Budget value={240} /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A component is a function that returns JSX | |
const Aaa = () => ( | |
<div> | |
You can do any JSX in here | |
</div> | |
) | |
// Remember to export the component | |
// Most people have one component per file and use export default | |
export default Aaa |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Simple request | |
async function sendRequest(url) { | |
try { | |
const response = await fetch(url) | |
if( response.status !== 200 ) { | |
console.log('Request wasn't successful. Code: ' + response.status) | |
return null | |
} | |
const data = await response.json() | |
return data // success |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Option 1: use a promise directly | |
let data; | |
function itTakesTime1() { | |
return new Promise((resolve, reject) => { | |
/* do something that takes time and produces a value */ | |
resolve(value) | |
}) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import readline from 'node:readline' | |
async function ask(query) { | |
const rl = readline.createInterface({ input: process.stdin, output: process.stdout, tabSize: 4 }); | |
return new Promise((resolve) => rl.question(query, (answer) => { | |
rl.close(); | |
resolve(answer); | |
})); | |
} |
NewerOlder