A
A
Adel Khalitov2019-02-23 00:14:44
MongoDB
Adel Khalitov, 2019-02-23 00:14:44

Why does nodejs throw an error lead.save is not a function?

The node receives data from the client, processes it and must save it. But here's the annoyance:
lead.save is not a function
Maybe the problem is in the mongoose schema?:

LeadSchema  = new mongoose.Schema({
  leadId: String,
  leadStatus: String,
  firmName: String,
  contactPhones: Array,
  contactName: String,
  position: String,
  contactEmail: String,
  address: String,
  lprsName: String,
  comments: Array,
  tasks: Array,
  link2gis: String,
  createdDate: String
});

exports.Lead = conn.model('Lead', LeadSchema);

Data received:
{ leadId: '5cef12b4-28f6-6ca7-edd2-c4a242d99986',
  leadStatus: 'attemptCommunicateLPR',
  firmName: '44',
  contactPhones: [ '3434' ],
  contactName: '',
  position: '',
  contactEmail: '',
  address: '43343',
  lprsName: '',
  comments:
   [ { description: '', createdDate: '2019-02-22T20:22:22.789Z' } ],
  tasks:
   [ { status: 'started',
       action: 'meet',
       description: '43434',
       createdDate: '2019-02-22T20:22:22.790Z',
       deadLineDate: '2019-02-26T20:22:36.000Z' } ],
  link2gis: '',
  createdDate: '2019-02-22T20:22:22.790Z' }

It seems not, the keys and their types are the same.
Request processing itself:
var db = require('../config/index');
.....
router.post('/newLeadOffer', function(req, res) {
    db.Lead.findOne({leadId: req.body.Lead.leadId})
        .then(data => {
            if (data === null) {
                var lead = new db.Lead();
                lead = req.body.Lead;
                console.log(lead);
                lead.save(function(err) { 
                   if (err) { return console.log('Save Lead error ', err); }
                   console.log('Lead added to db');
                   res.send('Лид создан успешно').status(200);
                   return true;
                });
            }
            res.send('Лид с таким ID уже существует');
            return false;
        })
        .catch(err=> {
            console.log('This error: ' + err);
        })
})

Looks like it doesn't either. I believe that the mistake is that I did not specify the types in the data nested in the arrays. Is it so?
LeadSchema  = new mongoose.Schema({
  leadId: String,
  leadStatus: String,
  firmName: String,
  contactPhones: [String],
  contactName: String,
  position: String,
  contactEmail: String,
  address: String,
  lprsName: String,
  comments: [{description: String, createdDate: String}],
  tasks: [{
    status: String,
    action: String,
    description: String,
    createdDate: String,
    deadLineDate: String
  }],
  link2gis: String,
  createdDate: String
});

No, the mistakes continue. What could it be?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question