Mongoose - Save array of strings
I am trying to save an array of strings into my Database using Mongoose.
So for that, I have declared a variable of a person schema:
var newPerson = new Person ({
tags: req.body.tags
});
The schema itself looks like:
var personSchema = new mongoose.Schema({
tags: Array
});
For saving, refer the following command
newPerson.save(function(err) {
//basic return of JSON
});
With the help of Postman, I send in an array in the body but when I check the database, it only displays one entry with the array as a whole, you can refer to the screenshot:
Any ideas what I have to do?
In mongoose schema array of strings, you can pass strings as follows:
var personSchema = new mongoose.Schema({
tags: [{
type: String
}]
The Postman sends the 'array' as the string so for that you can simply verify by inspecting the kind of req.body.tags. So for that refer the following command:
console.log(typeof req.body.tags)
If the above command yields a string data type then you have to set the type of the content from Postman to JSON.