Chaosy
Tutorial Reviewer
- Joined
- Jun 9, 2011
- Messages
- 13,242
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.