使用Node.js时出错

时间:2012-10-26 11:36:29

标签: node.js mongodb mongoose

这是我的模特。

UserApiSchema.statics.createApi = function(user,fn){
  var instance = new UserApi();
  instance.user = user;
  instance.apiKey = "asdasdacasdasasd";
  console.log("username is " + user.username);
  instance.save(function(err){
    fn(err,instance);
  });
};

UserSchema.statics.newUser = function (email, password,username, fn) {
    var instance = new User();
    var apiInstance = new UserApi();
    instance.email = email;
    instance.password =password;
    instance.username = username;

    instance.save(function (err) {
        fn(err, instance);
    });
};

这是我的controller-users.js:

app.post(     ' /注册/&#39 ;,     function(req,res){          {的console.log(req.body.username);             User.newUser(

            req.body.email, req.body.password,req.body.username,
            function (err, user) {
                if ((user)&&(!err)) {
                    console.log(user.username)

                    UserApi.createApi(
                            user,function(err,userapi){
                                if((!err)){
                                    res.send("APi created")
                                }
                                else{
                                    if(err.errors.apiKey){
                                        res.send(err)
                                    }
                                }


                            });
                    req.session.regenerate(function(){
                        req.session.user = user._id;
                        res.send("Success here!"); 

                    });
                } else {
                    if (err.errors.email) {
                          res.send(err) 
                          console.log(req.body.password);
                          console.log(req.body.email);
                          console.log(req.body);
                        }                           
                   if (err.errors.username) {
                      res.send(err) 
                      console.log(req.body.password);
                      console.log(req.body.email);
                      console.log(req.body);
                    }   
                }
            });


    } 
});

概念是一旦接受了用户名/密码,API密钥就会与用户名一起存储。虽然,用户名有效负载被接受,但当我执行UserApiSchema调用以生成api时,不会生成这样的api。也没有错误。

1 个答案:

答案 0 :(得分:1)

可能是真正的基础......但是,你创建了所需的对象吗?

  • UserApiSchema = {};
  • UserApiSchema.statics = {};
  • UserApiSchema.statics.createApi = function(user,fn){...}

如果是这样......他们是否在模块中? 你从模块中导出了它们吗?

  • exports.userApiSchema = UserApiSchema;

您是否在controller-users.js中导入了它们?

  • var userApiSchema = require('./ UserApiSchema.js');