首页 > 解决方案 > Waterline .create() 不是函数

问题描述

使用 Sails.js 和 Waterline-ORM 开发一个简单的 REST,现在在 Post-request 上尝试在 orm 中创建一个简单对象时面临Post.create 不是函数问题。

模型:

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

控制器

  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);
    }
}

我已经检查了文档和官方 gh-issues,但是没有工作建议,我不明白我在做什么错

标签: javascriptsails.jsnodeswaterline

解决方案


有时,当编辑器自动加载其他Post文件而不是sails 全局模型时,我会遇到这种情况,即const Post = require('../models/Post');在控制器文件的顶部。


推荐阅读