使用 GET 请求时邮递员返回空

时间:2021-05-10 08:36:44

标签: javascript mongodb api

我正在使用 mongodb、js、express 和 postman 开发 RESTful API 来测试它。我正在尝试连接到我的云图集 mongodb 集群并检索所有信息。但是,在邮递员上测试时,它返回空:[]

我可以很好地连接到数据库,并且我的收藏中有 3 个文档。数据库名为mobile_phone_store,集合名为personal_information

这是我的 server.js

const express = require("express");
const mongoose = require("mongoose");
const infoRoute = require("./app/routes/db.routes.js");

const app = express();

app.use(express.json());

mongoose.connect(
"mongodb+srv://*************************/mobile_phone_store?retryWrites=true&w=majority", {
    useNewUrlParser: true,
    useFindAndModify: false,
    useUnifiedTopology: true
  }
);

app.use(infoRoute);

app.listen(3000, () => {
console.log("Server is running...");
}); 

这是我的 db.model.js

  const mongoose = require('mongoose');
 // create a mongoose schema for a quotation
const PersonalInfoSchema = mongoose.Schema({
    customerID: String,
    Title: String,
    Fname: String,
    Lname: String,
    mobile: String,
    email: String
},     {

    timestamps: true
});
 // export the model to our app

 const personal_information = mongoose.model("personal_information", PersonalInfoSchema);

 module.exports = personal_information;

这是我的 db.routes

  const express = require("express");
  const Model = require("../models/db.model");
  const app = express();

 app.get("/personal_informations", async(request, response) => {
     const infos = await Model.find({});

     try {
         response.send(infos);
    } catch (error) {
         response.status(500).send(error);
    }
    });

 module.exports = app;

在邮递员中,我使用的网址为 http://localhost:3000/personal_informations。有任何想法吗?我一直在寻找解决方案一段时间,但我什么也没找到

1 个答案:

答案 0 :(得分:0)

问题可能出在 MongoDB 连接上。

尝试在 mongoose .connect( 'mongodb+srv://*************************/mobile_phone_store?retryWrites=true&w=majority', { useNewUrlParser: true, useFindAndModify: false, useUnifiedTopology: true, } ) .then(() => { app.listen(3000, () => { console.log('Server is running...'); }); }) .catch((err) => console.log(err)); 解析时进行监听:

std::array<std::array<int, 3>, 2> array;
for (/*const*/ std::array<int, 3> /*&*/ row : array) {
    for (int /*&*/ e : row) {
        // ...
    }
}
for (/*const*/ auto /*&*/ row : array) {
    for (auto /*&*/ e : row) {
        // ...
    }
}