简体   繁体   中英

How to pass data from a class into a Mongoose model?

I'll be going through what I have done to solve this problem.

  1. There is an empty board ( here ).
  2. You can place the knight piece anywhere on the board.
  3. You have to choose a desired position.
  4. The Game should return the coordinates of the shortest distance it takes to get to that desired position
  5. For Example. Let's place the knight on A1. I want to get to C5. The coordinates that would be returned are: A1, B3, C5.

Easy Enough?

  • This game has to be designed using a RESTFUL api with NodeJS
  • There will be no interface . It is purely back-end.
  • I am using a chess library as I am finding it difficult to create my own board (etc..) You can find it here: https://github.com/jhlywa/chess.js

What have I done to solve this?

I created a controller and a board class

This code does not work but is shown to simulate what I want to achieve:

(I am an amateur technically).

My Controller:

app.post("/", (req, res) => {
  var board = new Board(chess = new Chess());
  // The post request creates a board
  board.clearBoard();
  // This clears the board of existing pieces
  board.placeFirstPosition(position);
  // This adds the knight to the board
  board.endPosition(position);
  // This method in the class here will handle the logic of moving the knight to the end position 
// This logic needs to be passed into the model 
  var move = new Move({
  coordinate: (all_the_coordinates)
  })
  // This data (coordinates) will be passed into the model (via mongoose)
  move.save().then((doc) => {
    res.send(doc);
  }, (e) => {
    res.status(400).send(e);
  });
  // Finally these moves will be saved to the database and will be returned to the server
});

My Board Class:

var Chess = require('../../node_modules/chess.js').Chess;

function Board(chess = new Chess()) {
  this._chess = chess;
}

Board.prototype.clearBoard = function() {
  this._chess.clear();
}

Board.prototype.placeFirstPosition = function(position) {
  this._chess.put({ type: 'k', color: 'w' }, position)
}

Board.prototype.endPosition = function(position) {
 // logic calculating the sequence of coordinates
}

Board.prototype.showBoard = function() {
  return this._chess.ascii();
}

module.exports = {Board};
  • It may seem a bit long winded that I have added this chess library which can display the board but this was my only option as I am unsure how to create one otherwise.

My Problem:

The questions are:

  • How can I pass the coordinates from my method in my class into my move model? This subsequently will be saved in the database.
  • How can I pass the desired position into the functions in the POST without hardcoding it? Such as board.placeFirstPosition('a1');
  • I'll be handling the logic later on, what route would you recommend?
  • I can't use postman to pass any data in as the class is doing that.
  • Is it necessary to have a database ? My thinking is there has to be otherwise how will we be able to know what coordinates it took to get to that position?

Your Advice:

  • If you can think of a better way of me achieving this end result, I would love to hear your advice.

Assuming I am getting this correct, your conditions are:

  • Initial position is chosen
  • Destination position is chosen
  • An HTTP request is made and returns the quickest path.

Stages would be to

Create an algorithm to match your logic

function findBestPath(initPosX, initPosY, destPosX, destPosY)

Create a route to use this function

const express = require('express');
const app = express();
const bodyParser = require('bodyParser');
app.use(bodyParser.json());

app.post('/getBestPath', (req, res) => {
    let initPosX = req.body.initPosX,
        initPosY = req.body.initPosY,
        destPosX = req.body.destPosX,
        destPosY = req.body.destPosY;

    res.send(findBestPath(initPosX, initPosY, destPosX, destPoxY));
};

app.listen(3000, () => console.log('Example app listening on port 3000!'))

Once you're done with that you can can call this function using a POST HTTP request from any interface you can think of (including postman).

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