0

I created an instance function on a Model as per this spec:

describe('Instance methods', function() {
    var task;

    beforeEach(function() {
      return Task.create({
        name: 'task',
      }).then(function(_task) {
        task = _task;
      });
    });

    describe('addChild', function() {
      it('should return a promise for the new child', function() {
        return task.addChild({ name: 'task2' }).then(function(child) {
          expect(child.name).to.equal('task2');
          expect(child.parentId).to.equal(task.id);
        });
      });
    });
..... more not applicable
  });

This is it, but I am not getting zilch back...

Task.prototype.addChild = function(task) {
  return Task.create(task.name).then(function(task) {
    console.log('task', task);
    return task;
  });
};

As mentioned in the spec:

should return a promise for the new child

And as per the console:

Task Instance methods addChild should return a promise for the new child:

And this is the error I am getting back:

SequelizeValidationError: Validation Error

So what I am doing wrong to not add this task in the data base? Am I not using the create method correctly?

Thanks in advance!

UPDATE

I tried this:

Task.prototype.addChild = function(task) {   var task = Task.build({
name: task['name'] });   return task.save(); };

But I am getting back,

AssertionError: expected null to equal 1

2 Answers 2

1

they are some error i think in your implementation :

According to your test, a task is describe with a name and a parentId. but when you create your task you pass at the addChild { name: 'toto' } and you passed in Task.create directly task.name which can't work, the method attempt to received an object.

So insteadOf that

Task.prototype.addChild = function(task) {
    return Task.create(task.name).then(function(task) {
        console.log('task', task);
        return task;
    });
};

Try this :

Task.prototype.addChild = function(task) {
    return Task.create(task).then(function(task) {
        console.log('task', task);
        return task;
    });
};

// Than you call like this
await Task.addChild({
    name: 'name',
    parentId: 1
});

// OR

Task.addChild({
    name: 'name',
    parentId: 1
}).then(() => // something);
Sign up to request clarification or add additional context in comments.

Comments

0

The create method accepts an optional argument, which is the resulting object of the create method. In addition to assigning the new task instance's name to task.name, you'll also have to pass in the parentId which would be the foreign key to the new task instance. Using "1" will help you pass this spec, but you'll want to use this.id (this, being the initial instance the addChild method is being called on).

Task.prototype.addChild = function(task) {
  return Task.create({
    name: task.name,
    parentId: this.id
  });
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.