[英]How to pass data from a class into a Mongoose model?
我將介紹解決該問題的方法。
容易嗎?
我做了什么來解決這個問題?
我創建了控制器和板類
該代碼不起作用,但顯示為模擬我想要實現的功能:
(從技術上講我是業余愛好者)。
我的控制器:
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
});
我的董事會班級:
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};
我的問題:
問題是:
board.placeFirstPosition('a1');
你的建議:
假設我正確了,您的條件是:
階段將是
創建算法以匹配您的邏輯
function findBestPath(initPosX, initPosY, destPosX, destPosY)
創建使用此功能的路線
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!'))
完成后,您可以從可以想到的任何接口(包括郵遞員)中使用POST HTTP請求來調用此函數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.