模式是否必须完全匹配才能返回任何内容? (猫鼬)

时间:2019-05-25 10:02:09

标签: node.js reactjs mongodb express mongoose

我有一个非常简单的后端服务器和客户端,希望显示mongo数据库中的数据,在名为test的数据库中,有一个名为cards的集合中的文档。 当我从该文件运行正则表达式时,它给了我一个文档,但是当我通过后端的api搜索它时,它却没有给我任何东西,没有错误,只是在控制台中显示了“ true”和“ data:”。

cconst mongoose = require("mongoose");
const express = require("express");
var cors = require("cors");
const bodyParser = require("body-parser");
const logger = require("morgan");
const Data = require("./data");
const Card = require("./card");

const API_PORT = 3001;
const app = express();
app.use(cors());
const router = express.Router();

    // this is our MongoDB database
    const dbRoute =
      "mongodb+srv://dbUser:PASSWORD@cluster0-jhfnc.mongodb.net/test?retryWrites=true";

    // connects our back end code with the database
    mongoose.connect(dbRoute, { useNewUrlParser: true });

    let db = mongoose.connection;

    db.once("open", () => console.log("connected to the database"));

    // checks if connection with the database is successful
    db.on("error", console.error.bind(console, "MongoDB connection error:"));

    // (optional) only made for logging and
    // bodyParser, parses the request body to be a readable json format
    app.use(bodyParser.urlencoded({ extended: false }));
    app.use(bodyParser.json());
    app.use(logger("dev"));

    // this is our get method
    // this method fetches all available data in our database
    router.get("/getCardsByName/:cardName", (req, res) => {
      const { name } = req.params.cardName;
      Card.find({ name: { $regex: ".*" + name + ".*" } }, (err, cards) => {
        if (err) return res.json({ success: false, error: err });
        console.log("data: " + cards);
        return res.json({ success: true, cards: cards });
      });
    });

    // append /api for our http requests
    app.use("/api", router);

    // launch our backend into a port
    app.listen(API_PORT, () => console.log(`LISTENING ON PORT ${API_PORT}`));

客户端如下所示:

// /client/App.js
import React, { Component } from "react";
import axios from "axios";

class App extends Component {
  // initialize our state
  state = {
    cards: [],
    id: 0,
    message: null,
    intervalIsSet: false,
    idToDelete: null,
    idToUpdate: null,
    objectToUpdate: null,
    cardToSearch: null
  };

  // when component mounts, first thing it does is fetch all existing data in our db
  // then we incorporate a polling logic so that we can easily see if our db has
  // changed and implement those changes into our UI
  componentDidMount() {
    //this.getDataFromDb();
    if (!this.state.intervalIsSet) {
      let interval = setInterval(this.getDataFromDb, 1000);
      this.setState({ intervalIsSet: interval });
    }
  }

  // never let a process live forever
  // always kill a process everytime we are done using it
  componentWillUnmount() {
    if (this.state.intervalIsSet) {
      clearInterval(this.state.intervalIsSet);
      this.setState({ intervalIsSet: null });
    }
  }

  getCardByCardName = card_name => {
    fetch(`http://localhost:3001/api/getCardsByName/${card_name}`)
      .then(cards => cards.json())
      .then(res => this.setState({ cards: res.cards }));
  };

  render() {
    const { cards } = this.state;
    return (
      <div>
        <ul>
          {cards.length <= 0
            ? "No Cards Found"
            : cards.map(card => (
                <li style={{ padding: "10px" }} key={card.id}>
                  <span style={{ color: "gray" }}> id: </span> {card.id} <br />
                  <span style={{ color: "gray" }}> data: </span>
                  {card.name}
                </li>
              ))}
        </ul>
        <div style={{ padding: "10px" }}>
          <input
            type="text"
            style={{ width: "200px" }}
            onChange={e => this.setState({ cardToSearch: e.target.value })}
            placeholder="Card name"
          />
          <button
            onClick={() => this.getCardByCardName(this.state.cardToSearch)}
          >
            FIND
          </button>
        </div>
      </div>
    );
  }
}

export default App;

模式如下:

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

// this will be our data base's data structure
const CardSchema = new Schema(
  {
    _id: Schema.Types.ObjectId,
    armor: Number,
    artist: String,
    attack: Number,
    cardClass: String,
    classes: Array,
    collectible: Boolean,
    collectionText: String,
    cost: Number,
    dbfld: Number,
    durability: Number,
    elite: Boolean,
    entourage: Array,
    faction: String,
    flavor: String,
    health: Double,
    hideStats: Boolean,
    howToEarn: String,
    howToEarnGolden: String,
    id: String,
    mechanics: Array,
    multiClassGroup: String,
    name: String,
    overload: Number,
    playRequirements: Object,
    questReward: String,
    race: String,
    rarity: String,
    referencedTags: Array,
    set: String,
    spellDamage: Double,
    targettingArrowText: String,
    text: String,
    type: String
  },
  { timestamps: true }
);

// export the new Schema so we could modify it using Node.js
module.exports = mongoose.model("Card", CardSchema);

1 个答案:

答案 0 :(得分:1)

事实证明我使用的结构不正确。应该是:

router.get("/getCardsByName/:cardName", (req, res) => {
  **const name = req.params.cardName;**
  console.log(name);
  Card.find({ name: { $regex: ".*" + name + ".*" } }, (err, cards) => {
    if (err) return res.json({ success: false, error: err });
    console.log("data: " + cards);
    return res.json({ success: true, cards: cards });
  });
});