无法访问来自MongoDB的数据?

huangapple go评论89阅读模式
英文:

Unable to access data from mongoDB?

问题

我按照教程进行了操作,通过mongoose连接到了mongoDB,但出于某种原因,console.log(data)没有显示任何内容。
英文:

I followed a tutorial, connected to mongoDB via mongoose, but for some reason the console.log(data) doesn't show anything.

const mongoose = require('mongoose');
const mongoURI = 'mongodb+srv://gofood:xxxxxxxxx@cluster0.bgpaa3e.mongodb.net/?retryWrites=true&w=majority'

process.on('uncaughtException', err => {
  console.log('UNCAUGHT EXCEPTION! 💥 Shutting down...');
  console.log(err.name, err.message);
  process.exit(1);
});

async function called(){
  console.log('Connection successful!');
  const fetched_data = await mongoose.connection.db.collection("foodCategory");
  console.log('reaching here?????')
  fetched_data.find({}).toArray(function(err, data){
    if(err) console.log(err);
    else console.log(data);})
};

const mongoDB = async () => {
  await mongoose
    .connect(mongoURI, {
      useNewUrlParser: true
    })
    .then(() => called())
}

module.exports = mongoDB;

output:-

[nodemon] starting `node .\index.js`
Example app listening on port 5000
Connection successful!
reaching here?????

In the tutorial the data comes up instantly. Is there something that I'm not doing?

答案1

得分: 1

尝试使用 await 直到找到所有数据

// language: lang-js
async function called(){
  console.log('连接成功!');
  const fetched_data = await mongoose.connection.db.collection("foodCategory");
  console.log('是否到达这里?????')
  await fetched_data.find({}).toArray(function(err, data){
    if(err) console.log(err);
    else console.log(data);})
};
英文:

Try await until u find all the data

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

async function called(){
  console.log(&#39;Connection successful!&#39;);
  const fetched_data = await mongoose.connection.db.collection(&quot;foodCategory&quot;);
  console.log(&#39;reaching here?????&#39;)
  await fetched_data.find({}).toArray(function(err, data){
    if(err) console.log(err);
    else console.log(data);})
};

<!-- end snippet -->

答案2

得分: 0

你可以像这样修改 called() 函数:

async function called() {
  console.log('连接成功!');
  const FoodCategory = mongoose.model('FoodCategory', new mongoose.Schema({}));
  const data = await FoodCategory.find().exec();
  console.log(data);
}

也许会起作用
英文:

You can modify the called() function like this:

async function called() {
console.log(&#39;Connection successful!&#39;);
const FoodCategory = mongoose.model(&#39;FoodCategory&#39;, new mongoose.Schema({}));
const data = await FoodCategory.find().exec();
console.log(data);
}

May be it's work!!

答案3

得分: 0

这是我修复它的方式。
我在我的函数中提供了集合名称,但没有提供数据库名称,后来我发现在MongoDB的URL中,你必须提到数据库名称。

旧的URL

新的URL

英文:

This is how i fixed it.
I was wondering that in my function i'm providing my collection name but not database name. later i found that in the mongoDB url you have to mention the database name

old url

const mongoURI = &#39;mongodb+srv://gofood:xxxxxxxxx@cluster0.bgpaa3e.mongodb.net/?retryWrites=true&amp;w=majority&#39;

new url

const mongoURI = &#39;mongodb+srv://gofood:xxxxxx@cluster0.bgpaa3e.mongodb.net/**gofoodmern**?retryWrites=true&amp;w=majority&#39;

huangapple
  • 本文由 发表于 2023年8月4日 04:02:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831301.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定