Mongoose Model.find is not a function?

后端 未结 5 918
温柔的废话
温柔的废话 2020-12-15 19:40

Spent hours trying to figure this out - I\'m adding a new Model to my app but it\'s failing with \"TypeError: List.find is not a function\". I have another model, Items, th

5条回答
  •  庸人自扰
    2020-12-15 20:00

    To import model instance and call method, for example

    modelfile.js

    const mongoose = require('mongoose');
    const Schema = mongoose.Schema;
    
    const NotificationSchema = new Schema({
        count: Number,
        color: String,
        icon: String,
        name: String,
        date: {type: Date, default: Date.now },
        read: Boolean
    });
    
    module.exports = mongoose.model('notifications', NotificationSchema);
    

    queryfile.js

    const Notification = require('./models/model-notifications');
    
    function totalInsert(online) {
      let query = { name: 'viewed count' };
      Notification.find(query,(err,result)=>{
        if(!err){
          totalCount.count = online + result.length;
          totalCount.save((err,result)=>{
            if(!err){
              io.emit('total visitor', totalCount);
            }
          });
        }
      });
    }
    

提交回复
热议问题