簡體   English   中英

為什么對MongoDB的mongoose查詢返回空數組?

[英]Why mongoose query to MongoDB return empty array?

我在MongoDB中為查找文檔創建了mongoose模式,但是它不起作用,總是返回一個空數組

在MongoDB中,我有一個包含此類文檔的集合

{
   "_id":"5cba6a9079beee20a817b532",
   "name":"Soborniy, 151",
   "imageURL":"http://localhost:3001/locations/zaporozhya_soborniy_151.jpg",
   "lat":47.845174,
   "lng":35.127215,
   "mainImageURL":"http://localhost:3001/points/Zaporozhya_Soborniy_151.jpg",
   "pointName":"Jays Zaporizhia: Soborniy 151",
   "pointDescription":"This is our European flagship store. For this store we chose an area called Kreuzber, a multicultural melting pot of artists, hipsters and all things Berlin. In this vast 150m² open space, we showed our respect to the greats of German design by creating a shop interior in the style of iconic Braun designer Dieter Rams.  Our seating area, bean cellar, shop counter are all a homage to his amazing craft and ingenuity. The store also has a large kitchen, which is a new challenge for our brand.  We hired two young chefs, Tyler from America, and Paulo from Italy, to cook locally grown, fresh, simple and healthy breakfast and lunch menu. As for coffee, %Arabica Berlin is filled with our usual passion for serving the best beans with the upmost dedication.  Our founder, Kenneth owns a coffee farm in Hawaii, and we trade green beans from all over the world.  We even offer beans from Ninety Plus Coffee, who are creating barista champions in so many countries the world over. This store is filled with our passion for coffee, food, design, and seeing the world. Please visit us and let’s ”See the World Through Coffee” together",
   "neighborhoodPoints":[
      {
         "_id":"5cba6e034ca43420a82f7313",
         "name":"Soborniy, 172",
         "imageURL":"http://localhost:3001/locations/zaporozhya_soborniy_172.jpg"
      },
      {
         "_id":"5cba6ec54ca43420a82f7314",
         "name":"Soborniy, 175",
         "imageURL":"http://localhost:3001/locations/zaporozhya_soborniy_175.jpg"
      }
   ]
}

創建了用於從集合中獲取文檔的mongoose Schema

var mongoose = require("mongoose");

const locationSchema = mongoose.Schema({
  _id: mongoose.Schema.Types.ObjectId,
  name: String,
  imageURL: String,
  lat: Number,
  lng: Number,
  mainImageURL: String,
  pointName: String,
  pointDescription: String,
  neighborhoodPoints: [
    {
      _id: mongoose.Schema.Types.ObjectId,
      name: String,
      imageURL: String,
    },
  ],
});

const Location = mongoose.model("location", locationSchema, "locations");

module.exports = Location;

表達路由器來處理請求

const express = require("express");
const router = express.Router();

const Location = require("../models/location_schema");

router.get("/", (req, res) => {
  Location.find().exec((err, locations) => {
    if (err) return res.status(500).json(err);
    res.status(200).json(locations);
  });
});

module.exports = router;

為什么我的路線會退回?

[]

您需要在find調用中指定一個空對象:

Location.find({}).exec(...);

哦,我的數據庫中的集合名稱“點”,但我將我的模式命名為“locations”。 這是數據庫查詢的問題。

const Points = mongoose.model("point", pointSchema, "points");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM