【发布时间】:2020-05-05 07:36:10
【问题描述】:
我遇到了一个奇怪的错误。我猜这是因为我没有正确处理错误以获取更多信息。这是我得到的错误。我在这里搜索过,但没有成功找到类似的东西。文档让我很困惑。
{"name":"MongoError","level":"error","service":"user-service"}
这是我的连接脚本:
const mongoose = require('mongoose'),
logger = require('./logging');
async function connect(){
await mongoose.connect("mongodb://localhost:27017/fdData")
.catch(error => {
logger.error(error);
});
}
async function disconnect(){
await mongoose.disconnect()
logger.info('DB disconnected');
}
module.exports= {connect, disconnect}
}
module.exports= {connect, disconnect}
这就是我所说的。 (这只是脚本的一个小sn-p)。
更新于 20 年 1 月 20 日@太平洋标准时间上午 1024 点。
- (从 weather.js 中删除了 db.connect() 和 db.disconnect。db.connect 发生在 app.js 上。 weather.js 检查猫鼬就绪状态并报告 状态 1(已连接)。
- 添加了 mongoose Op 结果到记录器,所以现在我们可以看到 在控制台中尝试了猫鼬操作。
//schema setup
const weatherSchema = new mongoose.Schema({
condition: String,
temp: Number,
windDir: String,
windSpd: Number,
windGust: Number,
windChill: String,
humidity: Number,
icon: String,
tempIcon: String,
date: String,
updated: String,
});
const weatherWarningSchema = new mongoose.Schema({
warning: String,
warningStart: String,
warningExpire: String,
warningBody: String,
id: String,
date: String,
});
const Weather = mongoose.model("weather", weatherSchema);
const Warning = mongoose.model("warning", weatherWarningSchema);
currentWeather=[];
async function saveWeather(update){
/////////commented Out for troubleshooting//////////
// await db.connect()
// .then(error=>{logger.error(error)
/////////end comments/////////////
Weather.findOneAndUpdate({date: update.date},
{ date:update.date,
condition:update.condition,
temp:update.temp,
windChill:update.windChill,
windDir:update.windDir,
windSpd:update.windSpd,
windGust:update.windGust,
humidity:update.humidity,
icon:update.icon,
tempIcon:update.tempIcon,
updated:update.updated},
{upsert:true,
new: true,},
function(error, result){
if (error){
logger.error(error);
} else{
console.log(result);
logger.info('Weather Saved to DB '+moment().utcOffset(-8).format('HH:mm'))
}
})
console.log('Mongoose Ready State: '+mongoose.connection.readyState)
///////commented out for troubleshooting////////
// }).then((res, error)=>{
// if(error){
// logger.error(error)
// }
// db.disconnect();
// })
/////////end comments/////////
}
这是控制台中完整的初始输出:
正在获取刻录数据...
正在更新天气数据...
(node:3890) DeprecationWarning:当前的服务器发现和监控引擎已被弃用,并将在未来的版本中删除。要使用新的服务器发现和监控引擎,请将选项 { useUnifiedTopology: true } 传递给 MongoClient 构造函数。
(node:3890) DeprecationWarning:当前的 URL 字符串解析器已被弃用,并将在未来的版本中删除。要使用新的解析器,请将选项 { useNewUrlParser: true } 传递给 MongoClient.connect。
{"message":"Dashboard Server 运行在 3000","level":"info","service":"user-service"}
{"message":"Crewsense 令牌是当前的","level":"info","service":"user-service"}
{"message":"A911 令牌是当前的","level":"info","service":"user-service"}
Mongoose 就绪状态:1
{ _id: 5e25ef3d8c936f22f7722326, 日期:'2020-01-20', __v: 0, 条件:'晴天', 湿度:96, 图标:'sunny.png', 温度:34, tempIcon: 'https://cdn.aerisapi.com/wxicons/v2/cold.png', 更新:'01/20/20 10:19', 风寒:'34', windDir: 'N', 阵风:0, 风速:0 }
{"message":"Weather Saved to DB 10:19","level":"info","service":"user-service"}
正在保存刻录数据...
刻录数据已保存。
【问题讨论】: