英文:
TypeError: Cannot read properties of undefined (reading 'length') when using mongoose
问题
以下是您提供的代码部分的翻译:
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
mongoose.connect('mongodb://localhost:27017/todolistDB');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
const itemsSchema = new mongoose.Schema({
name: String
});
const Item = mongoose.model("Item", itemsSchema);
const item1 = new Item({name: "Workout"});
const item2 = new Item({name: "skydive"});
const item3 = new Item({name: "bungee"});
const defaultItems = [item1, item2, item3];
Item.insertMany(defaultItems, function(err){
if (err){
console.log(err + "yea");
} else {
console.log("good to go");
}
});
app.get("/", function(req, res) {
res.render("list", {listTitle: "Today", newListItems: items});
});
app.listen(3000, function() {
console.log("Server started on port 3000");
});
请注意,您的代码中存在一些问题,可能会导致错误。特别是在app.get("/", function(req, res) {...}
部分中,items
未定义。您可能需要在路由处理程序中获取和渲染数据库中的项目列表。如果您需要有关特定错误的帮助,可以提供更多上下文。
英文:
i'm making a todolist (angela yu), i'm trying to write to a new database, but i keep getting error.this is the error:
C:\Users\tunji\Desktop\webdev\todolist-v2-starting-files\node_modules\mongoose\lib\model.js:3519
for (let i = 0; i < error.writeErrors.length; ++i) {
^
TypeError: Cannot read properties of undefined (reading 'length')
at C:\Users\tunji\Desktop\webdev\todolist-v2-starting-files\node_modules\mongoose\lib\model.js:3519:47
at collectionOperationCallback (C:\Users\tunji\Desktop\webdev\todolist-v2-starting-files\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:133:26)
at Timeout.<anonymous> (C:\Users\tunji\Desktop\webdev\todolist-v2-starting-files\node_modules\mongoose\lib\drivers\node-mongodb-native\collection.js:160:11)
at listOnTimeout (node:internal/timers:564:17)
at process.processTimers (node:internal/timers:507:7)
this is the code:
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
mongoose.connect('mongodb://localhost:27017/todolistDB');
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
// const itemsSchema = {
// name: String
// };
const itemsSchema = new mongoose.Schema({
name: String
});
const Item = mongoose.model("Item", itemsSchema);
const item1 = new Item({name: "Workout"});
const item2 = new Item({name: "skydive"});
const item3 = new Item({name: "bungee"});
const defaultItems = [item1, item2, item3];
Item.insertMany(defaultItems, function(err){
if (err){
console.log(err + "yea");
} else {
console.log("good to go");
}
});
app.get("/", function(req, res) {
res.render("list", {listTitle: "Today", newListItems: items});
});
app.listen(3000, function() {
console.log("Server started on port 3000");
});
i've checked the server, deleted and installed the node module ,everything, still no fix,nothing at all
答案1
得分: 3
不要使用localhost:27017,而是使用127.0.0.1:27017。
所以应该像这样:
mongoose.connect("mongodb://127.0.0.1:27017/todolistDB")
英文:
Instead of using localhost:27017
Rather use 127.0.0.1:27017
So it should look like this:
mongoose.connect("mongodb://127.0.0.1:27017/todolistDB")
答案2
得分: 0
我认为您无法连接到mongoose,根据mongoose文档,connect是一个异步方法。
尝试等待连接建立,然后写入数据库
示例:
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.get("/", function(req, res) {
res.render("list", {listTitle: "Today", newListItems: items});
});
app.listen(3000, function() {
console.log("Server started on port 3000");
});
async function connect() {
try {
await mongoose.connect('mongodb://localhost:27017/todolistDB');
console.log('Connected to MongoDB');
} catch (error) {
console.log('Could not connect to MongoDB');
throw error;
}
const itemsSchema = new mongoose.Schema({
name: String
});
const Item = mongoose.model("Item", itemsSchema);
const item1 = new Item({name: "Workout"});
const item2 = new Item({name: "skydive"});
const item3 = new Item({name: "bungee"});
const defaultItems = [item1, item2, item3];
Item.insertMany(defaultItems, function(err){
if (err){
console.log(err + "yea");
} else {
console.log("good to go");
}
});
}
connect();
英文:
I think you cannot connect to mongoose, as per mongoose documentation connect is an async method.
Try waiting for connection to be established, then write to the database
example:
const express = require("express");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.get("/", function(req, res) {
res.render("list", {listTitle: "Today", newListItems: items});
});
app.listen(3000, function() {
console.log("Server started on port 3000");
});
async function connect() {
try {
await mongoose.connect('mongodb://localhost:27017/todolistDB');
console.log('Connected to MongoDB');
} catch (error) {
console.log('Could not connect to MongoDB');
throw error;
}
const itemsSchema = new mongoose.Schema({
name: String
});
const Item = mongoose.model("Item", itemsSchema);
const item1 = new Item({name: "Workout"});
const item2 = new Item({name: "skydive"});
const item3 = new Item({name: "bungee"});
const defaultItems = [item1, item2, item3];
Item.insertMany(defaultItems, function(err){
if (err){
console.log(err + "yea");
} else {
console.log("good to go");
}
});
}
connect();
``
</details>
# 答案3
**得分**: 0
为了解决这个问题,你首先需要注释掉`item.insert`和你的数组。
```javascript
const itemsSchema = new mongoose.Schema({
name: String
});
const Item = mongoose.model("Item", itemsSchema);
const item1 = new Item({name: "Workout"});
const item2 = new Item({name: "skydive"});
const item3 = new Item({name: "bungee"});
// const defaultItems = [item1, item2, item3];
// Item.insertMany(defaultItems, function(err){
// if (err){
// console.log(err + "yea");
// } else {
// console.log("good to go");
// }
之后,在终端中运行app.js
,然后切换到Mongo或Mongosh,输入show dbs,你应该会看到todolistDB已经存在。现在,你可以取消注释你的其余代码,并再次运行app.js
。
英文:
To solve this problem you have first to comment the tiem.insert and your array
const itemsSchema = new mongoose.Schema({
name: String
});
const Item = mongoose.model("Item", itemsSchema);
const item1 = new Item({name: "Workout"});
const item2 = new Item({name: "skydive"});
const item3 = new Item({name: "bungee"});
// const defaultItems = [item1, item2, item3];
// Item.insertMany(defaultItems, function(err){
// if (err){
// console.log(err + "yea");
// } else {
// console.log("good to go");
// }
after that run the app.js in the terminal, now move to the mongo or mongosh and write show dbs, you should now see the todolistDB already. so now you could go and uncomment the rest of your code and run thr app.js again
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论