Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
444 views
in Technique[技术] by (71.8m points)

json - Can't acces specific values in my node.js mongoose Model (only the Object)

I have been working at this for the past 4 hours. I would therefore like some help. I want to access the specific values in my database, eg. as response.data.values.imglink although when adding imglink in console.log() I get undefined. I can get the general object but not the specifik values.

I have defined my Song Schema as:

const songSchema = new Schema({
    values: [{
    imglink: {
      type: String
    },
    id: {
      type: String
    },
    spotify: {
      type: String,
    },
    soundCloud: {
      type: String,
    },
    youtube: {
      type: String,
    },
    appleMusic: {
      type: String,
    }}
  ],
}, {
    timestamps: true,
})

As you can see values is an array of objects. People with a similiar problem on here hadn't included the correct values in their Schema, so maybe that's my problem? Although to me it looks correct. I then GET the values in my database. The JSON object usually looks something like this:

    [
  {
    "_id": "5ffbba4dc47e847a79c9c68f",
    "values": [
      {
        "_id": "5ffbba4dc47e847a79c9c690",
        "imglink": "imagelink",
        "id": "id",
        "soundCloud": "soundcloudvalue",
        "youtube": "youtubevalue",
        "appleMusic": "applemusicvalue",
        "spotify": "spotifyvalue"
      }
    ]
  }
]

I call it by this function, which is supposed to print out the individual values:

      const getAllSongs = () => {
    axios.get('http://localhost:5000/songs/'+id)
    .then(function (response) {
      console.log(response); // returns an object
      console.log(response.data.values.imglink); // returns an object  
    })
    .catch(function (error) {
        // handle error
        console.log(error);
    })
  }

I have an Express route object that allows me to access a song by it's id as GET http://localhost:5000/songs/id in the VS-code HTTP client (similiar to postman):

    router.get(`/:id`, function(req, res) {
    return Song.find(
        {"values.id": req.params.id}
    ).then(function(song) { 
        // return orders when resolved
        res.send(song);
        console.log(id);
        res.json('works yesss');
    })
    .catch(function (err) {
        // handle error
        res.status(400).json('Error: '+err)
    })
});

Here are some popular solutions I have tried:

Wrapping response in JSON.stringify() doesn't work.

toObject() and toJSON() don't work either as they aren't defined when I use them. the _doc hack doesn't work either.

I have tried looking at the Schema which is where I think the problem is. The POST-request adds the right data, the GET-request goes through I just can't acces the specific values.

I hope you have the time to help, thanks. I will be extremely grateful. And of course let me know if you have any questions.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

the result of find() is a Array so to access the desired key, if length of result Array is one, to access the desired key is response.data[0].values[0].imglink.

note: the values key is a array of obejct

If the array size is more than one, you want to see the result, you can use map() if it's not worked, using lean() like this

    router.get(`/:id`, function(req, res) {
    return Song.find(
        {"values.id": req.params.id}
    ).lean().then(function(song) { 
        // return orders when resolved
        res.send(song);
         console.log(song[0].values[0].imglink); // type of song is array of object [] and values is array
        res.json('works yesss');
    })
    .catch(function (err) {
        // handle error
        res.status(400).json('Error: '+err)
    })
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...