如何从我的ejs模板中调用app.js中定义的函数?

时间:2022-10-31 22:14:34

I have a function defined in app.js

我在app.js中定义了一个函数

var addUser = function(uname, pword, e_mail, name, callback){

var coll_model =  mongoose.model('collaborator', User);
var collaborator = new coll_model(
{
    username: uname,
    password : pword,
    email : e_mail,
    fullName : name
});

collaborator.save(function (err){
    if(err)
    {
        console.log('There was an error creating the user');
    }
});

callback(collaborator._id);

};

I want to be able to call this from my ejs template.

我希望能够从我的ejs模板中调用它。

How does one go about this?

怎么会这样呢?

Thanks in advance

提前致谢

1 个解决方案

#1


1  

This is a server side operation. You should never invoke a function on the client that manipulates data directly at the database.

这是服务器端操作。您永远不应该在客户端上调用直接在数据库中操作数据的函数。

As you are using node and monogoDB, you could consider using express as your webframework. You can read about the communication between client -> server -> database here: http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/

当您使用node和monogoDB时,您可以考虑使用express作为您的webframework。您可以在此处阅读客户端 - >服务器 - >数据库之间的通信:http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/

#1


1  

This is a server side operation. You should never invoke a function on the client that manipulates data directly at the database.

这是服务器端操作。您永远不应该在客户端上调用直接在数据库中操作数据的函数。

As you are using node and monogoDB, you could consider using express as your webframework. You can read about the communication between client -> server -> database here: http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/

当您使用node和monogoDB时,您可以考虑使用express作为您的webframework。您可以在此处阅读客户端 - >服务器 - >数据库之间的通信:http://coenraets.org/blog/2012/10/creating-a-rest-api-using-node-js-express-and-mongodb/