I'm getting an odd error. I'm guessing it is because I'm not handling the error correctly to get more information. Here is the error I am getting. I have searched on here without success in finding something similar. The docs are just confusing me.
{"name":"MongoError","level":"error","service":"user-service"}
Here is my connection script:
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}
And here is where I am calling it. (This is just a small snippet of the script).
UPDATED 1/20/20 @ 1024am PST.
//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/////////
}
This is the complete initial output in console:
Getting Burn Data...
Updating Weather Data...
(node:3890) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
(node:3890) DeprecationWarning: current URL string parser is deprecated, and will be removed in a future version. To use the new parser, pass option { useNewUrlParser: true } to MongoClient.connect.
{"message":"Dashboard Server is running on 3000","level":"info","service":"user-service"}
{"message":"Crewsense token is current","level":"info","service":"user-service"}
{"message":"A911 Token is Current","level":"info","service":"user-service"}
Mongoose Ready State: 1
{ _id: 5e25ef3d8c936f22f7722326, date: '2020-01-20', __v: 0, condition: 'Sunny', humidity: 96, icon: 'sunny.png', temp: 34, tempIcon: ' https://cdn.aerisapi.com/wxicons/v2/cold.png ', updated: '01/20/20 10:19', windChill: '34', windDir: 'N', windGust: 0, windSpd: 0 }
{"message":"Weather Saved to DB 10:19","level":"info","service":"user-service"}
Saving Burn Data...
Burn Data Saved.
The error is telling you db disconnected
, whats happening is that you're not waiting for the promise returned from the updateOne
function.
You should add an await
there to fix this.
await db.connect()
.then(async (error) {
logger.error(error)
console.log('Saving Weather... '+mongoose.connection.readyState) //"Saving Weather...1"
await Weather.updateOne({date: update.date},
... )
}).then((res, error)=>{
if(error){
logger.error(error)
}
db.disconnect();
})
I figured it out!!!
Mongoose and MongoDB were operating correctly. The issue was in the assignment of the currentWeather
array. I was pushing the new data onto the array instead of replacing it. So my script was continuously placing currentWeather[0]
into my DB which is why it would work once and not again.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.