TypeError:无法读取属性' post'在Nodejs中未定义

时间:2016-03-19 22:29:29

标签: javascript node.js post get typeerror

我收到以下错误:

/**
 * Implements hook_theme
 *
 * @param $existing
 * @param $type
 * @param $theme
 * @param $path
 * @return array
 */
function MYMODULE_theme($existing, $type, $theme, $path)
{
  return array(
    'name_of_theme_function' => array(
      'variables' => array('name_of_array' => array()),
      'template' => 'name-of-my-template',
      'path' => drupal_get_path('module', 'MYMODULE') . '/theme'
    ),
   )
}

/**
 * My Module Callback
 *
 * @return string
 * @throws \Exception
 */
function MYMODULE_callback_function(){
  $output = array('array value', 'array value 2');

  $output = array(
    '#theme' => 'name_of_theme_function',
    '#name_of_array' => $output
  );

  return render($output);
}

/*** Inside the template name-of-my-template.tpl.php
dpm($name_of_array)

为此:

app.post('/api/assignments/user', createUser);
   ^

TypeError: Cannot read property 'post' of undefined

at Object.<anonymous> 
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at module.exports 
at Object.<anonymous> 

编辑:从此文件中调用它:

module.exports = function(app, userModel) {
"use strict";

app.post('/api/assignments/user', createUser);
app.get('/api/assignments/user', findUsers);
app.get('/api/assignments/user/:id', findUserById);
app.put('/api/assignments/user/:userId', updateUserById);
app.delete('/api/assignments/user/:userId', deleteUserById);

function createUser(req, res) {
    var newUser = userModel.createUser(req.body);
    res.json(newUser);
}

function findUsers(req, res) {
    if (req.query.username) {
        if (req.query.password) {
            findUserByCredentials(req, res);
        } else {
            findUserByUsername(req, res);
        }
    } else {
        var users = userModel.findAllUsers();
        res.json(users);
    }
}

function findUserById(req, res) {
    var userId = parseInt(req.params.id);
    var user = userModel.findUserById(userId);
    res.json(user);
}

function findUserByUsername(req, res) {
    var username = req.query.username;
    var user = userModel.findUserByUsername(username);
    res.json(user);
}

function findUserByCredentials(req, res) {
    var credentials = {
        username: req.query.username,
        password: req.query.password
    };
    var user = userModel.findUserByCredentials(credentials);
    res.json(user);
}

function updateUserById(req, res) {
    var userId = parseInt(req.params.id);
    var newUser = userModel.updateUser(userId, req.body);
    res.json(newUser);
}

function deleteUserById(req, res) {
    var userId = parseInt(req.params.id);
    userModel.deleteUser(userId);
    res.send(200);
}

}();

我不明白为什么我会为此获得module.exports = function (app) { "use strict"; var userModel = require("./models/user.model.js"); var formModel = require("./models/form.model.js"); require("./services/user.service.server.js")(app, userModel); require("./services/form.service.server.js")(app, formModel); require("./services/field.service.server.js")(app, formModel); };

2 个答案:

答案 0 :(得分:2)

您最后通过(*count)++;调用该函数function(app, userModel) {...。它什么也没有回报。因此,您可以}()获得undefined

答案 1 :(得分:0)

在开始使用“app”

之前,您需要确保执行类似的操作
var app = express();