'mongoose find method nothing to return but findOne have data to return
const deviceSchema = mongoose.Schema({
deviceId: {type: String, index: true},
alias: String,
deviceType:String,
status: Number,
temperature: Number,
updateTime: {type: Date, default: Date.now}
}, {autoIndex: false});
deviceSchema.index({ deviceId: 1, status: -1 });
Domain.models.device.find({"deviceId":requestBody.deviceID});
use find method nothing to return
Domain.models.device.findOne({"deviceId":requestBody.deviceID});
use findOne method will return data
I don't know why findOne have the return data and find have nothing to return. And I try find method in PowerShell it has return value.
Solution 1:[1]
When there are no matches find() returns [], while findOne() returns null.
Domain.models.device.find( {"deviceId":requestBody.deviceID}, function (err, results) {
if (err) { ... }
if (!results.length) {
// do stuff here
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ankit Ranjan Sinha |
