Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Undefined is not a function - Sails.js

So I'm currently learning Sails.js through following some tutorials and I have encountered this problem many times now.

I tried searching for the solutions but none of them seemed to work.

module.exports = {
signup: function(req, res) {
    var username = req.param("username");
    var password = req.param("password");

    Users.findByUsername(username).done(function(err, usr){
        if (err) {
            res.send(500, { error: "DB Error" });
        } else if (usr) {
            res.send(400, {error: "Username already Taken"});
        } else {
            var hasher = require("password-hash");
            password = hasher.generate(password);

            Users.create({username: username, password: password}).done(function(error, user) {
                if (error) {
                    res.send(500, {error: "DB Error"});
                } else {
                    req.session.user = user;
                    res.send(user);
                }
            });
        }
    });
  }
}

The problems seems to be on this line, it says that:

undefined is not a function.

Users.findByUsername(username).done(function(err, usr)

It seems to me that the problem is on the .done because when I try to make it like this:

Users.findByUsername(username, function(err, usr)

it works.

This is my Users.js under model

module.exports = {
attributes: {
    username: 'STRING',
    password: 'STRING'
 }
};

Any idea on how to fix this?

Also in chrome console the status code is :

500 Internal Server Error

Where could be the problem?

like image 835
bless1204 Avatar asked May 22 '15 07:05

bless1204


2 Answers

May be you are following some tutorial which is written for previous version of SailsJS. In previous version there was a function named done, which is replaced by exec in newer versions. Try replacing done with exec. Then the code will be like,

module.exports = {
signup: function(req, res) {
    var username = req.param("username");
    var password = req.param("password");

    Users.findByUsername(username).exec(function(err, usr){
        if (err) {
            res.send(500, { error: "DB Error" });
        } else if (usr) {
            res.send(400, {error: "Username already Taken"});
        } else {
            var hasher = require("password-hash");
            password = hasher.generate(password);

            Users.create({username: username, password: password}).done(function(error, user) {
                if (error) {
                    res.send(500, {error: "DB Error"});
                } else {
                    req.session.user = user;
                    res.send(user);
                }
            });
        }
    });
  }
}
like image 118
taufique Avatar answered Oct 27 '22 02:10

taufique


This is a common problem caused by the fact that the .done() function is now deprecated in sails.js and it's probably removed but you can replace it with .exec()function.

That's why the Exception TypeError: Undefined is not a function is raised here, because this function does no longer exists and belongs to old versions of sails.js and it's now Deprecated.

You can find more abiout it in this discussion here.

So your code should be:

Users.findByUsername(username).exec(function(err, usr){
like image 25
cнŝdk Avatar answered Oct 27 '22 02:10

cнŝdk