【问题标题】:NodeJs - Mongoose seem to not pass any query on my code, however I have effectively defined a query to be executedNodeJs - Mongoose 似乎没有在我的代码上传递任何查询,但是我已经有效地定义了一个要执行的查询
【发布时间】:2019-03-07 00:57:52
【问题描述】:

我会使用 model.find 将在我的数据库中找到的数据传递给一个值。首先,我会 console.log() mongoose 返回的值以确保它是相关值。但是目前,当我在我的 Nodejs 数据库中传递 find query 时,什么也没有发生。不会发生 console.log 或错误。 所以我想知道是什么坏了,如何让这些元素正常工作,得到我的日志,然后是我的价值。

这里是我的 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) 

这里是我的 server.js:

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

任何提示都会很棒, 谢谢

【问题讨论】:

  • 可以是任意数量的东西。连接不正确,get 请求不正确等需要更多代码来确定。
  • 连接有效!现在一切正常
  • 好吧,现在函数返回给我 mongoose.find() 的函数定义如下Query { _mongooseOptions: {}, _transforms: [], _hooks: Kareem { _pres: Map {}, _posts: Map {} }, _executionCount: 0,

标签: javascript node.js mongoose


【解决方案1】:

server.js 更改为:

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)
})

顺便说一句find 方法是一个承诺,所以如果你想console.log 它是回调之外的结果,那么你需要这样做:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 2013-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-03
    相关资源
    最近更新 更多