简体   繁体   English

当我尝试执行节点js应用程序时出现错误:findById不是函数

[英]there is an error when I am trying to execute a node js app: findById is not a function

There is a error: 发生错误:

findById is not a function findById不是函数

when I'm trying to execute a node js app. 当我尝试执行节点js应用程序时。
This is my code, exclusively for the eCommerce website. 这是我的代码,专门用于电子商务网站。

const path = require('path');

const express = require('express');
const bodyParser = require('body-parser');

const errorController = require('./controllers/error');
const sequelize = require('./util/database');
const Product = require('./models/product');
const User = require('./models/user');

const app = express();

app.set('view engine', 'ejs');
app.set('views', 'views');

const adminRoutes = require('./routes/admin');
const shopRoutes = require('./routes/shop');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

//findById methode // findById方法

app.use((req, res, next) => {
  User.findById(1)
      .then(user => {
        req.user = user;
        next();
      })
      .catch(err => console.log(err));
});

//routes controller //路由控制器

app.use('/admin', adminRoutes);
app.use(shopRoutes);

app.use(errorController.get404);

Product.belongsTo(User, { constraints: true, onDelete: 'CASCADE' });
User.hasMany(Product);

//sequelize statements // sequelize语句

sequelize
  // .sync({ force: true })
  .sync()
  .then(result => {
    return User.findById(1);
    // console.log(result);
  })
  .then(user => {
    if (!user) {
      return User.create({ name: 'Max', email: 'test@test.com' });
    }
    return User;
  })
  .then(user => {
    // console.log(user);

//port configuration //端口配置

  app.listen(3000,() => console.log(`looking at local host port number 3000`));
})
.catch(err => {
  console.log(err);
});

This error means that findById is not defined in your user model. 此错误意味着您的用户模型中未定义findById。 You'll need to add and export this method in your './models/user' module. 您需要在“ ./models/user”模块中添加和导出此方法。

I think you want the Model method findByPk to "Search for a single instance by its primary key.") Try: 我认为您希望Model方法findByPk “通过其主键搜索单个实例。”)尝试:

app.use((req, res, next) => {
  User.findByPk(1)
    .then(user => {

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试使用 web3 在 Pancakeswap 上执行交换,但在计算交易成本时出现错误 - I am trying to execute a swap on Pancakeswap using web3, but I get an error when calculating transaction cost 我收到错误:编译错误:当我尝试在 Ember 中编写帮助程序 function 时,ifCond 不是帮助程序 - I am gettting the error: Compile Error: ifCond is not a helper when I am trying to write a helper function in Ember 我正在尝试运行 node.js,如何解决“找不到模块”错误? - I am trying to run node.js, how do I resolve "Cannot find module" error? 当我尝试在节点中运行函数时,为什么Onclick =“ function()”显示404? - why does Onclick=“function()” shows up 404 when I am trying to run a function in node? 对 Node JS 很陌生,我正在尝试发布到特定请求,但它让我出错 - Quite new to Node JS and I am trying to post to a specific request but it get me error 当我在 Hyper cmd 上将 app.js 作为节点服务器运行时,会出现“events.js:377” - "events.js: 377" occurs when I am running the app.js as a node server on Hyper cmd 当我发送消息时,我正在尝试创建 web 聊天应用程序在控制台中出现此错误 - Am trying to create a web chat app when i send a message am having this error in console 在节点服务器中部署ext js应用程序时出现错误 - Getting Error when I deploy ext js app in node server 我尝试部署 function 时出错 - Error while i am trying to deploy the function 我正在尝试返回 function 但显示错误 - I am trying to return function but showing error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM