构造函数& NodeJs中的getter无效

时间:2017-09-23 19:29:28

标签: node.js

您好我在构建器级别和getter上遇到问题,我正在创建一个存储在数据库中的小型消息应用程序。我使用了NodeJs,我创建了一个允许连接数据库并进行管理的类,

数据库包含一个“message”表,其中包含字符串“id”“content”“creatd_d” 这是我称之为message.js的类代码:

let connection = require("../config/connection")
let moment = require("moment")


class Message{

  constructor (row) { 
     return this.row = row
  }


  get content(){
    return this.row.content
  }

  get created_d(){
    return moment(this.row.created_d)
  }

  static create(content, cb){
    connection.query('INSERT INTO message SET content = ?, created_d = ?', [content, new Date()] , (err, results) => {
      if (err) throw err
      cb()

    });
  }


  static all(cb){
    connection.query('SELECT * FROM message order by created_d DESC', (err, rows) =>{ 
        if(err) throw err 
        cb(rows.map((row) => new Message(row))) }) }
  }


module.exports = Message

getter的目标是声明模块“时刻”,允许更改日期格式,但getter不再有效 有谁知道,这可以来自于什么?提前谢谢你

2 个答案:

答案 0 :(得分:1)

删除构造函数中return this.row = row的返回值。您正在破坏构造函数而不返回Message的实例。

答案 1 :(得分:0)

有关我的问题的更多信息;这是“index.ejs”页面:

<!DOCTYPE html>
<html>
<head>
    <title>Ma premier app de NodeJs</title>
    <link rel="stylesheet" type="text/css" href="/assets/Semantic/semantic.min.css">
</head>
<body>
    <div class="ui main container">
        <div class="ui fixed inverted menu">
            <a href="/" class="header item">Home</a>

        </div>
        <br>
        <br>
        <h1>Bienvenue sur ma premier page ne NodeJs</h1>
        <% if (locals.flash && locals.flash.error) { %>
        <div class="ui negative message">
            <%= flash.error %>
        </div>
        <% } %>

        <% if (locals.flash && locals.flash.success) { %>
        <div class="ui positive message">
            <%= flash.success   %>
        </div>
        <% } %>

        <form action="/" method="post" class="ui form">
            <div class="field">
                <label for="message">Message</label>
                <textarea name="message" id="message"></textarea>
            </div>
            <button type="submit" class="ui red labeled submit icon button">
                <i class="icon edit"></i> Send
            </button>
        </form>
        <br>
        <h3>Les Messages</h3>
        <% for (message of messages){ %>
        <div class="message-item">
            <div class="ui message">
                <%= message.content %>
                <div class="ui date"><%= message.created_d %></div>
            </div>
            <br>

        </div>
        <%}%>


    </div>

</body>
</html>

这是页面serveur.js

let express = require("express")
let bodyParser = require("body-parser")
let session = require('express-session'); // Charge le middleware de session

let app = express()

//Moteur de template
app.set('view engine', 'ejs')


//Middleware
app.use('/assets', express.static("public"))
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json())
app.use(session({
    secret: "monsession",
    resave: false,
    saveUninitialized: true,
    cookie: { secure: false }
}))
app.use(require('./middlewares/flash.js'))

// Les Routes
app.get('/', (req, res) =>{

    let Message = require("./models/message")
    Message.all(function(messages){
        res.render('pages/index', {messages: messages})
    })


})

app.post('/', (req, res)=>{
    // test de disponibilité de message et si il est vide !!
    if (req.body.message === undefined || req.body.message === '') {
        req.flash('error', "Vous n'avez pas poster votre message")
        res.redirect('/')
        // res.render("pages/index", {error: "Vous n'avez pas entré aucun message"})
    }else{
        let Message = require("./models/message")
        Message.create(req.body.message, function(){
            req.flash('success', "Merci !")
            res.redirect('/')
        })
    }
})



app.listen(8080)

thaks