3

I have a Schema:

exports.participant = function(mongodb){
  var participantSchema =  new mongoose.Schema({
   uniqueid: {type: String, unique: true},
   channel: String,
   conference: String,
   calleridnum: String,
   calleridname: String,
   event: String,
   status : { type: Boolean, default: 0 }
},{ collection: 'participants'});

  var participant = mongodb.model('Participant', participantSchema);
  return participant;
}

I have already tried how to update a object in mongodb via mongoose, but still it doesn't work for me.

I want to update status to 1

I am calling the update code here . .

the file location of my schema is: ../models/app.js

the file location of my update is: ../lib/update.js

This is my update code . .

var appInit = require('../app');
var logger = appInit.logger;
var basedir = appInit.basedir;
var connection = appInit.connection;


var mongodb = appInit.mongodb;

var handleAsteriskAmi = require(basedir+'/lib/handleasteriskami');
var asteriskAmi = new handleAsteriskAmi();

var handleHttpProc = require(basedir+'/lib/handlehttpproc');
var httpProc = new handleHttpProc();

function handleSocketio(){
  this.init = function(io){
    io.sockets.on('connection',function(socket){
      if(socket){
        logger.info("socket.io is connected");
      }
      socket.on('disconnect', function () {
        logger.error("Socket.io is disconnected");
      });
      socket.on('action', function(data){
        var result = JSON.parse(JSON.stringify(data));
        asteriskAmi.listenActionConfbridge(result,function(response){
          logger.info(JSON.stringify(response));
          if(response.event=="originate"){
                  io.sockets.emit("response",response);
          }
          if(response.event=="confbridgemute" || response.event=="confbridgeunmute"){
                  io.sockets.emit("response",response);
                  mongodb.model("Participant").update(
                  {channel: response.channel},
                  {$set: {status: 1}}
                  );
          }
          if(response.event=="confbridgelock" || response.event=="confbridgeunlock"){
                  io.sockets.emit("response",response);
          }
        });
      });

    });
Talha Awan
  • 4,573
  • 4
  • 25
  • 40

1 Answers1

0

I believe you need to pass in a mongoose object in your participant function in ../models/app.js that you can then use to convert the participantSchema into a Model. Thus in your ../models/app.js, you can implement the participant function as

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
exports.participant = function(){
    var participantSchema =  new Schema({
        uniqueid: {type: String, unique: true},
        channel: String,
        conference: String,
        calleridnum: String,
        calleridname: String,
        event: String,
        status : { type: Boolean, default: 0 }
     },{ collection: 'participants'});

    var participant = mongoose.model('Participant', participantSchema);
    return participant;
}

Call the participant model in your ../lib/update.js:

var appInit = require('../app');
var Participant = appInit.participant();
Participant.update(
    {channel: response.channel},
    {$set: {status: 1}}
);
chridam
  • 100,957
  • 23
  • 236
  • 235