无法读取未定义序列的属性'findAll'

时间:2019-06-19 20:06:47

标签: mysql node.js express sequelize.js

我是表达js和sequelizejs的新手。我成功迁移了数据库中的表,所以我猜连接很好。

这是我的代码。 https://github.com/Picks42/express-test 请查看此文件 https://github.com/Picks42/express-test/blob/master/models/user.js

然后评论这一个 https://github.com/Picks42/express-test/blob/master/controller/test.js

让我知道是什么问题。

2 个答案:

答案 0 :(得分:0)

您应该删除第三行中的.User字段。因为您已经从(defproject getfluentspanish "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :dependencies [[cheshire "5.8.1"] [cljs-ajax "0.8.0"] [clojure.java-time "0.3.2"] [com.cognitect/transit-clj "0.8.313"] [com.google.javascript/closure-compiler-unshaded "v20190528" :scope "provided"] [cprop "0.1.13"] [day8.re-frame/http-fx "0.1.6"] [funcool/struct "1.4.0"] [luminus-immutant "0.2.5"] [luminus-transit "0.1.1"] [markdown-clj "1.10.0"] [metosin/muuntaja "0.6.4"] [metosin/reitit "0.3.7"] [metosin/ring-http-response "0.9.1"] [mount "0.1.16"] [nrepl "0.6.0"] [org.clojure/clojure "1.10.1"] [org.clojure/clojurescript "1.10.520" :scope "provided"] [org.clojure/google-closure-library "0.0-20190213-2033d5d9" :scope "provided"] [org.clojure/tools.cli "0.4.2"] [org.clojure/tools.logging "0.4.1"] [org.webjars.npm/bulma "0.7.5"] [org.webjars.npm/material-icons "0.3.0"] [org.webjars/webjars-locator "0.36"] [org.webjars/webjars-locator-jboss-vfs "0.1.0"] [re-frame "0.10.6"] [reagent "0.8.1"] [ring-webjars "0.2.0"] [ring/ring-core "1.7.1"] [ring/ring-defaults "0.3.2"] [selmer "1.12.12"] [thheller/shadow-cljs "2.8.39" :scope "provided"]] :min-lein-version "2.0.0" :source-paths ["src/clj" "src/cljs" "src/cljc"] :test-paths ["test/clj"] :resource-paths ["resources" "target/cljsbuild"] :target-path "target/%s/" :main ^:skip-aot getfluentspanish.core :plugins [[lein-shadow "0.1.3"] [lein-immutant "2.1.0"] [lein-sassc "0.10.4"] [lein-auto "0.1.2"]] :sassc [{:src "resources/scss/screen.scss" :output-to "resources/public/css/screen.css" :style "nested" :import-path "resources/scss"}] :auto {"sassc" {:file-pattern #"\.(scss|sass)$" :paths ["resources/scss"]}} :hooks [leiningen.sassc] :clean-targets ^{:protect false} [:target-path "target/cljsbuild"] :shadow-cljs {:nrepl {:port 7002} :builds {:app {:target :browser :output-dir "target/cljsbuild/public/js" :asset-path "/js" :modules {:app {:entries [getfluentspanish.app]}} :devtools {:watch-dir "resources/public"}} :test {:target :node-test :output-to "target/test/test.js" :autorun true}}} :npm-deps [[core-js "^2.6.9"] [shadow-cljs "2.8.31"] [create-react-class "15.6.3"] [react "16.8.6"] [react-dom "16.8.6"] [react-beautiful-dnd "11.0.4"] [react-dnd "7.6.0"] [react-dnd-html5-backend "7.6.0"] [react-dnd-touch-backend "0.8.3"] [react-dnd-multi-backend "3.2.1"]] :profiles {:uberjar {:omit-source true :prep-tasks ["compile" ["shadow" "release" "app"]] :aot :all :uberjar-name "getfluentspanish.jar" :source-paths ["env/prod/clj" "env/prod/cljs"] :resource-paths ["env/prod/resources"]} :dev [:project/dev :profiles/dev] :test [:project/dev :project/test :profiles/test] :project/dev {:jvm-opts ["-Dconf=dev-config.edn"] :dependencies [[binaryage/devtools "0.9.10"] [cider/piggieback "0.4.1"] [expound "0.7.2"] [pjstadig/humane-test-output "0.9.0"] [prone "1.6.3"] [re-frisk "0.5.4.1"] [ring/ring-devel "1.7.1"] [ring/ring-mock "0.4.0"]] :plugins [[com.jakemccrary/lein-test-refresh "0.24.1"]] :source-paths ["env/dev/clj" "env/dev/cljs" "test/cljs"] :resource-paths ["env/dev/resources"] :repl-options {:init-ns user} :injections [(require 'pjstadig.humane-test-output) (pjstadig.humane-test-output/activate!)]} :project/test {:jvm-opts ["-Dconf=test-config.edn"] :resource-paths ["env/test/resources"]} :profiles/dev {} :profiles/test {}}) 文件中导出了User本身。 另外,我建议您不要弄乱变量名。 M_Bank变量不会自己说话

git push heroku master

答案 1 :(得分:0)

// all the models using your index.js loader
const models = require('../models');
// the user model, note the capital User since 
const M_Bank = models.User;

exports.getTest = function(req,res){
    return M_Bank
    .findAll()
    // don't use M_Bank here since you are getting an array of Instances of the Model
    .then(users => res.status(200).send(users))
    .catch((error) => {
      console.log(error.toString());
      res.status(400).send(error)
    });

    /* this will never execute because it is after the return
    exports.index = function (request, response, next) {
        response.json((M_Bank.findAll()));
    };
    */
};

如果您可以选择使用async/await,则会使代码更具可读性。

const models = require('../models');
const M_Bank = models.User;

exports.getTest = async function(req, res) {
  try {
    const users = await M_Bank.findAll();
    return res.status(200).send(users);
  } catch (err) {
    console.log(err.toString());
    return res.status(400).send(err);
  }
};