簡體   English   中英

Mongoose:集合中的數組屬性由 Model.find() 返回空,即使它在數據庫中有數據

[英]Mongoose: Array attribute inside a collection is returned empty by Model.find() even though it has data in the database

我正在嘗試使用 Mongoose 為我的數據庫之一創建測試查詢。 為此,我在 JSON 文件中創建了一些數據並將其導入到 MongoDB。 這是數據:

{
    "name": "SSS",
    "discordid": "61231237",
    "prefix": "!",
    "servers": [
        {
            "name": "DMG TTT",
            "type": "garrysmod",
            "host": "66.151.244.2",
            "vchannelid": "616413849679036530",
            "tchannelid": "616414488916393984"
        },
        {
            "name": "DMG Potpourri",
            "type": "garrysmod",
            "host": "192.223.27.68",
            "vchannelid": "616415352271667232",
            "tchannelid": "616415277713981441"
        }
    ]
}

如您所見,servers 屬性是一個數組,其中包含 2 個對象。 但是,當我使用以下代碼使用 Mongoose 查詢數據庫時:

Guild.find({ name: "SSS" }).exec((err, data) => { console.log(JSON.stringify(data)) })

控制台返回以下內容:

[{"servers":[],"_id":"5d675d80dd57df7e7d88e491","name":"SSS","discordid":"61231237","prefix":"!"}]

請注意,servers 數組返回為空,即使其中包含數據。 查看一些解決方案,有人建議我嘗試 JSON.stringify,但不管有沒有它都沒有區別。 為什么會發生這種情況?

編輯:這是模型文件:

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

//Discord Guild schema
const guildSchema = Schema({
    name: String,
    discordid: String,
    prefix: String,
    servers: [{
        name: String,
        type: String,
        host: String,
        vchannelid: String,
        tchannelid: String
    }]
})

module.exports = mongoose.model("Guild", guildSchema)

這是 MongoDB CLI 所做的相同查詢:

> db.guilds.find()
{ "_id" : ObjectId("5d675d80dd57df7e7d88e491"), "name" : "SSS", "discordid" : "61231237", "prefix" : "!", "servers" : [ { "name" : "DMG TTT", "type" : "garrysmod", "host" : "66.151.244.2", "vchannelid" : "616413849679036530", "tchannelid" : "616414488916393984" }, { "name" : "DMG Potpourri", "type" : "garrysmod", "host" : "192.223.27.68", "vchannelid" : "616415352271667232", "tchannelid" : "616415277713981441" } ] }

嘗試以下方法,

你的公會模型,

const guildSchema = Schema({
    name: {type: String},
    discordid: {type: String},
    prefix: {type: String},
    servers: [{
        name: {type: String},
        type: {type: String},
        host: {type: String},
        vchannelid: {type: String},
        tchannelid: {type: String}
    }]
})

首先將 JSON 對象保存在您的數據庫中,如下所示 - 您的控制器文件,確保格式與以下相同。

var guildModel = require('../models/guild.model');



req.body =  [{
        "name": "SSS",
        "discordid": "61231237",
        "prefix": "!",
        "servers": [
        {
            "name": "DMG TTT",
            "type": "garrysmod",
            "host": "66.151.244.2",
            "vchannelid": "616413849679036530",
            "tchannelid": "616414488916393984"
        },
        {
            "name": "DMG Potpourri",
            "type": "garrysmod",
            "host": "192.223.27.68",
            "vchannelid": "616415352271667232",
            "tchannelid": "616415277713981441"
        }
        ],

    }];
    var guild = new guildModel(req.body[0]);
    guild.save((err , userSaved)=>{
                if(err){
                    res.status(400).send(err);
                }
                else{
                    console.log("userSaved =========> " , userSaved);
                    res.json({user: userSaved});
                }
            }); 

然后應用以下代碼從數據庫中檢索數據。

guildModel.find({name : "SSS"})
        .exec((err , found )=>{
            if (err){
                res.send(err)
        }else{
                res.send(found);
            }
         });

暫無
暫無

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

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