Код: Выделить всё
Webiste Folder
| assets
| css
| javaScript
> popup-form.js
| images
| saaa
| config
| server
| node_modules
> .env
> index.js
> package-lock.js
> package.json
| index.html
< /code>
Это содержимое внутри файла 'index.js < /p>
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const app = express();
dotenv.config();
const PORT = process.env.PORT || 7000;
const MONGOURL = process.env.MONGO_URL;
console.log("MONGO_URL:", MONGOURL);
mongoose.connect(MONGOURL).then(() => {
console.log("Database connected");
app.listen(PORT, () => {
console.log('Server is running on ' + PORT);
})
}).catch((error) => console.log(error));
const updatesSchema = ({
name: String,
description: String,
Date: String
});
const UpdateModel = mongoose.model("updates", updatesSchema);
app.use(express.static('public', {
setHeaders: (res, path) => {
if (path.endsWith('.js')) {
res.setHeader('Content-Type', 'application/javascript');
}
}
}));
app.get("/getUpdates", async(req, res) => {
const updatesData = await UpdateModel.find();
res.json(updatesData);
});
function addUpdate(Update_name, description, time) {
const newData = new UpdateModel({
name: Update_name,
description: description,
Date: time
});
console.log("Attempting to save:", newData);
newData.save()
.then(() => console.log("Data saved successfully!"))
.catch((err) => console.error("Error saving data:", err));
}
function findUpdates() {
UpdateModel.find({})
.then((documents) => {
const dataArray = documents.map((doc) => doc.toObject());
return dataArray
})
.catch((err) => console.error("Error retrieving data:", err));
}
< /code>
Здесь я пытаюсь применить изменения в базе данных Mongoose < /p>
import('./server/index.js').then((module) => {
const title = document.getElementById("update_title").value.toLowerCase().trim();
const description = document.getElementById("update_description").value.trim();
const time = document.getElementById("current_time").innerHTML.trim();
module.addUpdate(title, description, time);
module.findUpdates().then((test) => {
console.log("Fetched updates:", test);
document.getElementById("update_title").value = "";
document.getElementById("update_description").value = "";
}).catch((err) => console.error("Error fetching updates:", err));
});
//addUpdate(document.getElementById("update_title").value.toLowerCase().trim(), document.getElementById("update_description").value.trim(), document.getElementById("current_time").innerHTML.trim());
document.getElementById("update_title").value = "";
document.getElementById("update_description").value = "";
mongooseerror: параметр uri to openuri () должен быть строкой, полученная «неопределенная». Убедитесь, что первый параметр для mongoose.connect () или mongoose.createconnection () - строка. Тем не менее, теперь ошибка вернулась независимо от нового митомопада запуска файла.>
Подробнее здесь: https://stackoverflow.com/questions/794 ... ross-files