我正在使用 mongoDB 和 mongoose 我創建了一個水果集合,我想列印水果名稱然后斷開與服務器的連接
const mongoose = require('mongoose');
mongoose.connect("mongodb://localhost:27017/fruitsDB", {
useNewUrlParser: true
});
const fruitSchema = new mongoose.Schema({ //schema: come vogliamo che gli eleemnti vengano sturtturati
name: String,
rating: {
type: Number,
min: 1,
max: 10
},
review: String
});
//creiamo un modulo
const Fruit = mongoose.model("Fruit", fruitSchema);
const fruit = new Fruit({
name: "Apple",
rating: 8,
review: "good"
});
//fruit.save();
const kiwi = new Fruit({
name: "Kiwi",
rating: 10,
review: "great"
});
Fruit.find(function(err, fruits) {
if (err) {
console.log(err);
} else {
mongoose.connection.close();
fruits.forEach(function(fruit) {
console.log(fruit.name);
});
}
});
我在 app.js 中寫了這個,然后我用命令“node app.js”編譯它終端給了我這個:
蘋果蘋果蘋果蘋果獼猴桃
(node:24352) UnhandledPromiseRejectionWarning: MongoExpiredSessionError: 不能使用在 Object.applySession 結束的會話 (C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\sessions.js:647: 16) 在 Connection.command (C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:185:36) 在 C:\Users\Feder\Desktop\ udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\sdam\server.js:176:18 在 Object.callback (C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib \cmap\connection_pool.js:266:13) 在 C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection_pool.js:474:29 在 C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection_pool.js:402:13 在回呼 (C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\ node_modules\mongodb\lib\cmap\connect.js:52:9) 在 C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connect.js:124:13 在MessageStream.messageHandler (C:\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:479:9) 在 MessageStream.emit (events.js:400:28) (用\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:479:9) 在 MessageStream.emit (events.js:400:28) (使用\Users\Feder\Desktop\udemy\mongooseFruit\node_modules\mongoose\node_modules\mongodb\lib\cmap\connection.js:479:9) 在 MessageStream.emit (events.js:400:28) (使用node --trace-warnings ...顯示警告的創建位置)(節點:24352)UnhandledPromiseRejectionWarning:未處理的承諾拒絕。這個錯誤要么是因為在沒有 catch 塊的情況下拋出了異步函式,要么是因為拒絕了一個沒有用 .catch() 處理過的承諾。要在未處理的承諾拒絕時終止節點行程,請使用 CLI 標志--unhandled-rejections=strict(請參閱https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode)。(rejection id: 1) (node:24352) [DEP0018] DeprecationWarning:不推薦使用未處理的承諾拒絕。將來,未處理的承諾拒絕將使用非零退出代碼終止 Node.js 行程。
uj5u.com熱心網友回復:
首先,您需要先將模型保存到資料庫中。
其次,您似乎find以錯誤的方式使用該方法。
const fruit = new Fruit({
name: "Apple",
rating: 8,
review: "good"
});
fruit.save();
const kiwi = new Fruit({
name: "Kiwi",
rating: 10,
review: "great"
});
kiwi.save();
Fruit.find({}).then((fruits) => {
mongoose.connection.close();
fruits.forEach(function(fruit) {
console.log(fruit.name);
});
});
另外我建議使用async/await來提高代碼可讀性。
uj5u.com熱心網友回復:
你可以嘗試這樣的事情:
const fruits = await Fruit.find({})
// do whatever you want with fruits
mongoose.connection.close();
看起來您正在嘗試關閉連接,同時仍在使用它。通過使用 await 您將在關閉連接之前等待查詢結束。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/321260.html
上一篇:獲取MongoDB時更改日期格式
下一篇:陣列未正確插入物件
