Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
229 views
in Technique[技术] by (71.8m points)

node.js - my api is not listing address, latitude and longitude

[enter image description here] here is my results[1]here is my data here my data

here I create my mongo schema using mongoose

 const UserScheme = new mongoose.Schema({

     title:{
         type: String,
         required: true,

     },
     description:{
         type: String,
         required: false
     },
     email:{
         type: String,
          unique :true,
          required: true,
         lowercase: true
     },

everything seems right here

    address:{
         type: String,
         required: false,
         select: false
     },
     latitude:{
         type: Number,
         required:false,
          select: false
     },
    longitude:{
         type: Number,
         required:false,
         select: false
      },
      
     createdAt:{
         type: Date,
         default: Date.now,
     }, 
 })

my pre-save code

 UserScheme.pre('save', async function(next){
     const hash = await bcrypt.hash(this.password, 10)
     this.password = hash

here I geocode

    const code = await geocoder.geocode(this.address)
    console.log(this.address)
    console.log(code)
    

here I get the latitude and longitude

     if(Array.isArray(code)&&code[0]){
             this.latitude = code[0].latitude
             this.longitude = code[0].longitude
}

here is my list

 router.get('/list', async (req, res) =>{
     try{
         const users = await user.find()

         return res.send({users})
     }catch(err){
         console.log(err)
         return res.status(400).send({error: 'Error listing project'})
     }


 })

for some reason it isnt showing the address, latitude and longitude, can you help me ?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

select : false makes field values not accessible , remove it or make it , select : true.

 address:{
     type: String,
     required: false,
     select: true
 },
 latitude:{
     type: Number,
     required:false,
      select: true
 },
longitude:{
     type: Number,
     required:false,
     select: true
  },

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...