Question

I'm new to Node.js and i'm trying to run a sort of insert query. Here is my code:

exports.savetea = function(req, res){

var quantity = req.query.quantity;

var Sequelize = require('sequelize');
var sequelize = new Sequelize('nodehmssm', 'root', 'root', {host: "localhost", port: 3306, dialect: 'mysql'});

var Tea = sequelize.import(__dirname + "/model/tea");

Tea.build({quantity: quantity, status: "active"}).save();

res.send("Tea added...");
};

My tea.js file is as follows:

module.exports = function(sequelize, DataTypes) {
return sequelize.define("tea", {
    id : DataTypes.INTEGER,
    quantity : DataTypes.INTEGER,
    status : DataTypes.STRING,
    created_at : DataTypes.DATE
});
};

Whenever I run the code, I get the error

TypeError: Object [object Object] has no method 'save'

Also to mention, the settings are good as I can run the login code select * from where...

What am I doing wrong?

Was it helpful?

Solution

I know this is an old question, but perhaps I can help someone that has that problem.

Actualy the problem is that you are calling a .save() method of something that doesnt have such a method.

This is a common mistake. You are not using the .build() method correctly. See example below.

What you are doing:

Tea.build({quantity: quantity, status: "active"}).save();

What you should be doing is this:

var actualObject = Tea.build({quantity: quantity, status: "active"});
actualObject.save() //etc...

You see an error because Tea is actualy an object of the model NOT a instace of that model that you just built.

Hope that helps.

OTHER TIPS

A Somewhat Misleading Error Message

In Sequelize, you cannot save an object with undefined properties.

Since your model had no defaultValue, and you didn't define id or created_at, the .build() method was not creating a .save() method on the object it returned.

For more on the documentation, checkout their documentation on built instances and this section on how to implement autoincrement.

The type error you are getting is stating the object Tea has no method called .save. In your code:

Tea.build({quantity: quantity, status: "active"}).save();

Tea is your object and build is calling an instance method called .save where you want to insert your JSON data. When you call either a class or instance method from a specific sequelize model, you will get the error you found.

I recommend for you to use a template for your sequelize models to help resolve this common mistake. Below is my suggestion:

modelTemplate.js

module.exports = function(sequelize, DataTypes) {                                                    
    return sequelize.define('User', {                                                             
        col1: DataTypes.STRING,
        col2: DataTypes.STRING                                                                
    },{                                                                                              
        classMethods: {                                                                              
            doSomething: function(successcb, errcb, request) {}                                             
       },                                                                                           
       instanceMethods: {                                                                           
           saveSomething: function(data) {}                                              
       }                                                                                            
     });                                                                                              
   };

For your specific answer. I would adjust your call to the model with the following lines:

 var data_json = {quantity: quantity, status: "active"};
 Tea.build().save(data_json);

tea.js

module.exports = function(sequelize, DataTypes) {                                                    
    return sequelize.define('tea', {                                                             
         id : DataTypes.INTEGER,
         quantity : DataTypes.INTEGER,
         status : DataTypes.STRING                                                                 
    },{                                                                                              
        classMethods: {                                                                              
            doSomething: function(successcb, errcb, request) {}                                             
       },                                                                                           
       instanceMethods: {                                                                           
           save: function(data) {}                                              
               var new_instance = Tea.build({
                    quantity: data[0].quantity,
                    status: data[0].status
            });         
            //saving instance
            // you can also build, save and access the object with chaining:                                                             
            new_instance
            .save()
            .success(function(){}).error("instance error in .save() method"); 
       }                                                                                            
     });                                                                                              
   };

Edit: made a correction to a comma in the template.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top