I am new to mongoose js so i wanted to create a contact feild only one time the user fills a form here is my contact model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const ContactSchema = new Schema({
fname: {
type: String,
required: true,
default:"fname"
},
lname: {
type: String,
required: true,
default:"lname"
},email:{
type:String,
required: true,
default:""
},
mobile:{
type:Number,
required:true,
default:""
},
title:{
type:String,
required:true,
default:""
}
});
module.exports = Contact = mongoose.model('Contact', ContactSchema);
here is my user model
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
// Link Schema
const LinkSchema = require('./Link').schema;
const ContactData=require('./conatct').schema;
// Create User Schema
const UserSchema = new Schema({
username: {
type: String,
required: true,
unique: true,
lowercase:true,
trim: true,
},
email: {
type: String,
required: true,
unique: true,
trim: true,
lowercase:true
},
password: {
type: String,
required: true,
trim: true
},
resetToken:{type:String},
expireToken:{type:Date},
date: {
type: Date,
default: Date.now
},
theme: {
type: Number,
default: 1
},
avatar: {
type: String,
default: 'uploads/default.png'
},
displayname:{
type:String,
default:"",
trim:true
},
bio:{
type: String,
default: "",
},
title:{
type: String,
default: "",
},
links: {
type: [LinkSchema]
},
contact:{
type:[ContactData]
}
});
module.exports = User = mongoose.model('user, UserSchema);
** suggest me some queries for creating contact details only once the user enters the data i have tried from user.contact.push({}) but it creates multiple object in contact array**
If I get you, you want the contact field to be a single object not an array of objects then change
//change
contact:{
type:[ContactData]
}
//to
contact:ContactData
// when you want to add data you just do something like
const user = new User({
name,etc...,
contact.mobile:user_mobile, etc..
})