简体   繁体   中英

NodeJs - Mongoose seem to not pass any query on my code, however I have effectively defined a query to be executed

I would pass the found data on my database using model.find to a value. Primarily I would console.log() the value returns by mongoose to ensure it's a relevant value. But currently when I'm passing a find query in my Nodejs database nothing happens. Neither console.log or error occurs. So I'm wondering what is broken and how make those element functionning correctly, to get my log, then my value.

here my mongoose.model:

const mongoose = require("mongoose");
const Schema= mongoose.Schema;

var ChatSchema = new Schema({
    ip:{required:true, type: String},
    message: {required:true, type: String},
    room:{required:true, type: String}
});

module.exports = mongoose.model("Chat", ChatSchema) 

here my server.js:

 var chatScan= Chat.find(ip, function (err, docs) { 
        if(err) return console.log(err)
        return docs.length
})
console.log("chatScan: ", chatScan)

Any hint would be great, thanks

Change server.js to:

var scannedIp = 12.34.56.78;
var chatScan = Chat.find({ ip: scannedIp }, function (err, docs) {
        if(err) return console.log(err)
        console.log(docs.length)
})

BTW , the find method is a promise, so if you want to console.log it's results outside of the callback, then you need to do something like so:

chatScan.then(docs => {
  console.log(docs)
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM