• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

[Javascript] Variable not array

Status
Not open for further replies.

Chaosy

Tutorial Reviewer
Level 40
Joined
Jun 9, 2011
Messages
13,183
JavaScript:
app.post("/upload/:id", function(req, res) {
    if (!req.files) {
        return res.status(400).send("No files were uploaded.");
    }

    let sampleFile = req.files.sampleFile;

    sampleFile.mv("./public/" + req.files.sampleFile.name, function(err) {
        if (err) {
            return res.status(500).send(err);
        }
        console.log("x");
        db.read(Model, {_id: req.params.id}).then(function(value) {
            let song = new Song(req.files.sampleFile.name, "./public/" + req.files.sampleFile.name);
            console.log(value);
            console.log(typeof value.songs);
            console.log(Array.isArray(value.songs));
            value.songs.push(song); //crashes here since songs is not an array apparently
            console.log(value);
            db.update(Model, {_id: req.params.id}, value).then(function(value) {
                console.log("----");
                console.log(value);
                console.log(value.songs);
            });
            res.redirect("/view/" + req.params.id);
        });
    });
});

I am basically trying to update a database entry.
In this case I have an array called "songs", which I push a Song into and then try to save the updated array.

JavaScript:
console.log(value);
console.log(typeof value.songs);
console.log(Array.isArray(value.songs));
value.songs.push(song);

The output from the console.logs:

x
[ { _id: 58eb4a86ce7fc4220c3196ee,
name: 'array test 222',
__v: 0,
songs: [ [Object] ] } ]
undefined
false

It writes out that songs is an array which contains one Obeject but when I check with isArray it returns false.
 
Status
Not open for further replies.
Top