【问题标题】:node.js get results from mysql query using custom modulenode.js 使用自定义模块从 mysql 查询中获取结果
【发布时间】:2019-05-06 12:03:35
【问题描述】:

如何获得选择查询结果? 我越来越空/null。另外,在阅读了一些文章后,我尝试了回调逻辑并尝试了回调函数,但到目前为止没有运气。

我的自定义模块所有代码都在下面。

module.exports = {
    all: function (cb) {

        con.query("SELECT * from `posts`", function (err, posts, fileds) {
            if (err) {
                return cb(null, err);
            } else {
                return cb(null, posts);
            }
            // console.dir(posts);
        });
    },
    store: function (req, res, next) {

        // get values..
        const title = req.body.title;
        const body = req.body.body;
        const filename = (req.file != null) ? req.file.filename : null;
        var sql = "INSERT INTO `posts` (`post_id`, `title`, `body`,`file`, `created_at`) VALUES (NULL, '" + title + "', '" + body + "', '" + filename + "', CURRENT_TIMESTAMP);";
        con.query(sql, function (err) {
            console.log('Error' + err);
        });

        next();

        // redirect
        res.redirect("/admin/post");
    },
    delete: function (req, res, next) {},
    edit: function (req, res, next) {},
    update: function (req, res, next) {}
}

我试过 cb(callback) 但它说

TypeError: cb 不是函数

没有运气获得结果。 请指导和帮助。

我也在像这样的另一个文件中获取所有函数数据

var postModel = require('../models/posts');
console.log("admin.js :" + postModel.all());

【问题讨论】:

    标签: javascript node.js express


    【解决方案1】:

    参数应该是回调函数,如下所示。

    postModel.all(function(response) {
        console.log(response);//you will get result here
    });
    

    【讨论】:

      【解决方案2】:

      创建了简单的回调函数,它解决了我的问题。请看下面的代码。

          module.exports = {
          all: function (callback) {
      
              const newLocal = function (err, posts) {
                  if (err) {
                      return callback(null, err);
                  }
                  else {
                      // console.dir(posts);
                      return callback(posts);
                  }
              };
              con.query("SELECT * from `posts`", newLocal);
          },
          store: function (req, res, next) {
      
              // get values..
              const title = req.body.title;
              const body = req.body.body;
              const filename = (req.file != null) ? req.file.filename : null;
              var sql = "INSERT INTO `posts` (`post_id`, `title`, `body`,`file`, `created_at`) VALUES (NULL, '" + title + "', '" + body + "', '" + filename + "', CURRENT_TIMESTAMP);";
              con.query(sql, function (err) {
                  console.log('Error' + err);
              });
      
              next();
      
              // redirect
              res.redirect("/admin/post");
          },
          delete: function (req, res, next) {},
          edit: function (req, res, next) {},
          update: function (req, res, next) {}
      }
      

      更新的代码是:

      const newLocal = function (err, posts) {
      if (err) {
       return callback(null, err);
      }
       else {
       // console.dir(posts);
       return callback(posts);
      }
      };
      con.query("SELECT * from `posts`", newLocal);
      

      【讨论】:

        猜你喜欢
        • 2017-07-11
        • 2019-03-11
        • 1970-01-01
        • 2013-08-24
        • 1970-01-01
        • 1970-01-01
        • 2014-02-05
        • 2017-04-25
        相关资源
        最近更新 更多