我正在寫一個小型服務器與快遞,mongo和貓鼬。我的代碼:貓鼬空白查詢找到所有,但具體的查詢失敗
const express = require('express');
const MongoClient = require('mongodb').MongoClient
const mongoose = require('mongoose');
const COLOURS = ["e6194b", "3cb44b", "ffe119", "0082c8", "f58231", "911eb4", "46f0f0", "d2f53c", "fabebe", "e6beff"];
var url = 'mongodb://localhost:27017/test';
mongoose.connect(url);
var app = express();
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
console.log('Database connected and open');
});
var deviceSchema = mongoose.Schema({
mac: String,
ip: String,
colourIndex: Number
});
var Device = mongoose.model('Device', deviceSchema);
app.get('/register', function (req, res) {
console.log("Registration: ");
console.log(" MAC: " + req.query.mac);
console.log(" IP : " + req.query.ip);
Device.find({ mac: /req.query.mac/ }, function(err, deviceList) {
if (err || deviceList.length == 0) {
console.log(" Cannot find " + req.query.mac + ": " + err);
Device.count({ mac: /req.query.mac/ }, function(err, count){
var index = count;
console.log("There are " + count + " matching");
var device = new Device({ mac: req.query.mac, ip: req.query.ip, colourIndex: index });
// device.save(function (err, device) {
// if (err) {
// res.status(500).send("Internal error");
// return console.error(err);
// }
// res.status(202).send(COLOURS[index]);
// });
res.status(202).send(COLOURS[0]);
});
} else {
console.log(deviceList);
res.status(202).send("Success");
console.log(" Colour index: %s", deviceList[0].colourIndex);
}
});
});
當我嘗試使用mac: /req.query.mac/
找到設備,它失敗EVERY時間。如果我將它留空,它確實會返回設備(我添加了重複項以檢查註釋掉的代碼)。
我已經試過看documentation和stack overflow
儘管答案可能會匹配,但我確實認爲我的職位上的人甚至不知道如何開始提問。 –