1

i have looked all around trying to find a solution for this but i cant seem to find it

i am trying to see if a certain row exist in my nedb database and if it doesnt exist insert something but if it does exist then just move along here is what i have tried

function newAgent(pcName){
    socket.broadcast.emit('newAgent', pcName)
    agentList.find({agentName: { $nin: pcName}}, function(err, docs) {  

    agentList.insert({agentName: pcName}, function (err) {});
});

}

now i might be doing something stupid but im new to nedb so i have no idea what to use

Nik Hendricks
  • 244
  • 2
  • 6
  • 29

1 Answers1

0

How about trying something like this

function newAgent(pcName){
    socket.broadcast.emit('newAgent', pcName)
    agentList.find({agentName: { $in: pcName}}, function(err, docs) { 
    if(null === docs){
       agentList.insert({agentName: pcName}, function (err) {});
        } else {
//since it exists you might want update
        agentList.update({
            pcName: pcName
          }, {
        $set: {
          //call fields to be updated
        }
      }, {}, callback);
    }

});

}
Mudassar
  • 3,135
  • 17
  • 22
  • i tried this the only thing i did different was i removed the callback thing at the end and it doesn't do anything when a user connects it just sits there – Nik Hendricks Jan 10 '18 at 12:46
  • can you try console printing docs and whether its going in the if or the else block. I have a strong hunch your ' socket.broadcast.emit('newAgent', pcName)' might be causing the problem – Mudassar Jan 10 '18 at 12:58
  • then can you try adding an undefined check instead of null check – Mudassar Jan 11 '18 at 06:34
  • I have done that it still wont work any suggestions? – Nik Hendricks Jan 16 '18 at 00:08