我试图从mongodb中读取数据,在Vue中使用mongoose,但是控制台返回err'mongoose.connect不是函数'。我不知道为什么会这样?我尝试单独运行服务器文件(mainDashStatus.js),它按预期运行。有帮助吗?
db.js
const mongoose = require('mongoose');
const url = 'mongodb://localhost:27017/dev6'
connect = () =>{
mongoose.connect(url, { useNewUrlParser: true }).then(console.log('Database Connected')).catch(err => {console.log(err)})
}
disconnect = () =>{
mongoose.disconnect().then(console.log('Database Disconnected')).catch(err=>{console.log(err)});
}
exports.connect = connect;
exports.disconnect = disconnect;
mainDashStatus.js
const Config = require('../model/config');
const dbConnect = require('./db').connect;
dbConnect();
checkStatus = (callback) =>{
Config.find({parentid:null}).exec((err, docs)=>{
callback(err, docs)
})
}
checkStatus((err, docs)=>{
status = true;
docs.forEach(element => {
if(element.latesStatus != 'success') status = false
});
console.log(status);
module.exports = status;
})
主脚本
<script lang="ts">
import { Component, Prop, Vue } from 'vue-property-decorator';
const checkMainDashStatus = require('../server/mainDashStatus');
export default {
data(){
return{
....
}
},
methods:{
status(){
if(checkMainDashStatus){
return "background-color: #00FF00;" // green
}
return "background-color: #FFFF00;"// yellow
},
}
}
</script>
答案 0 :(得分:0)
您正在将.then(...).catch(...)
函数与猫鼬一起使用,默认情况下它不支持。将Promises与猫鼬结合使用的安全方法是通过全局Promise。
const mongoose = require('mongoose');
const url = 'mongodb://localhost:27017/dev6'
mongoose.Promise = global.Promise;
connect = () =>{
mongoose.connect(url, { useNewUrlParser: true }).then(console.log('Database Connected')).catch(err => {console.log(err)});
}
...
...
希望对您有帮助。
答案 1 :(得分:0)
我遇到了类似的问题。正在使用
let mongoose = require("mongoose");
一旦我切换到
const mongoose = require("mongoose");
问题解决了。