Im trying to create a document and a subdocument separately. Schema:
const mongoose = require('mongoose');
require('./streets');
const PlaceSchema = new mongoose.Schema({
place1: String,
streets: [{type: mongoose.Schema.Types.ObjectId, ref: 'streetSchema'}]
});
module.exports = mongoose.model('place', PlaceSchema);
SubSchema:
const mongoose = require('mongoose');
const StreetSchema = new mongoose.Schema(
{
trafficLights: Number,
shops: Number,
busStops: Number,
},
{ strict: false }
);
module.exports = StreetSchema;
Then I try to instantiate streets by itself:
const place = // ...get a certain place from DB;
const street = new Street({
trafficLights: 1,
shops: 2,
busStops: 3,
});
place.streets.push(street)
and im getting
Street is not a constructor
How can I create instantiate the subSchema and push it into the 'streets' array?
PS - I also tried putting both of the Schemas in the same file, but couldnt reach the nested array (streets) without instantiating the entire "place" schema.
Thank you.
You should create a model, please add this line at the end of your schema file
mongoose.model('street', StreetSchema);
And import it wherever you need it
const Street = mongoose.model('street');