user11460219
user11460219

Reputation:

Waterline .create() is not a function

Developing a simple REST using Sails.js and Waterline-ORM, now facing Post.create is not a function problem while trying to create a simple object in orm on Post-request.

Model:

module.exports = {
  attributes: {
    title: {
      type: "string",
      required: true,
    },
    body: {
      type: "string",
      required: true,
    },
  },
};

Controller

  createPost: async (req, res) => {
    const title = req.body.title;
    const body = req.body.body;
    try {
      let newPost = Post.create({ title: title, body: body }).fetch();
    } catch (error) {
      console.log(newPost);
    }
}

I've already checked documentation and official gh-issues, but there are no working advice, I don't understand what am I doing wron

Upvotes: 1

Views: 863

Answers (2)

Jorge Vargas
Jorge Vargas

Reputation: 301

Sometimes it happens to me when the editor autoloads some other Post file instead of the sails global model, i.e. const Post = require('../models/Post'); at the top of the controller file.

Upvotes: 4

arbuthnott
arbuthnott

Reputation: 3819

As a first try, I would test using a different model name than Post. Sails has some pre-defined stuff already in the global name-space and sometimes there are overlaps. In my own project, I had errors trying to name a model File. I wouldn't be surprised if the same were true for Post.

Try renaming to something else and see if it fixes your issue.

Upvotes: 0

Related Questions