结果未存储在 Google 数据存储数据库中

奇酷

无法将数据保存在 Google Datastore DB 中没有收到任何错误,有人可以帮我找到修复程序吗

Console.log 结果如下

entityKey: Key { namespace: undefined, kind: 'User', path: [Getter] }, entityData: { firstname: 'Abcd', lastname: 'Abcd', email: '[email protected]', password: '123454 ', createdOn: 'Abcd', [Symbol(KEY)]: Key { namespace: undefined, kind: 'User', path: [Getter] } }, Ref - https://www.npmjs.com/package/gstore -节点

const express = require('express');
const router = express.Router();
const { check, validationResult } = require('express-validator');
var User =require('../models/user');

//get register page
router.get('/register',function(req,res){
    res.render('register')
});

//get login page
router.get('/login',function(req,res){
    res.render('login')
});


router.post('/register',  [
        check('Name').isEmpty().withMessage('The Name is required'),
        check('Email').isEmail().withMessage('Email is requried'),
        //check('Password').isEmpty().withMessage('pass is requried'),
        //check('Password','Password is Requried').isEmpty(),
       // check('Password2','Password Not Match').equals('password2'),
 
      ], (req, res,next) => {
  const errors = validationResult(req);
  if (!errors.isEmpty()) {
    res.render('register',{
        error:errors.mapped()
    })
  }else{

    console.log()
    
    const newUser = new User ({
        firstname:req.body.name,
        lastname:req.body.name,
        email :req.body.Email,
        password :req.body.Password,
        createdOn:req.body.name
      });
      console.log("Data1",newUser)
     
        const createUser = (req, res) => {
        const entityData = User.sanitize(req.body);
        const user = new User(entityData);
        console.log("Data2",createUser)
        user.save()
            .then((entity) => {
                res.json(entity.plain()); 
            })
            .catch((err) => {
                // If there are any validation error on the schema
                // they will be in this error object
                res.status(400).json(err);
            })
    };
  
      req.flash('success_msg','you are registered and can login now');
      res.redirect('/users/login');

  }
  
});

module.exports=router;

const { Gstore, instances } = require('gstore-node');
const { Datastore } = require('@google-cloud/datastore');
 
const gstore = new Gstore();
const datastore = new Datastore({
    projectId: 'sinuous250616',
});
 
gstore.connect(datastore);
 
// Save the gstore instance
instances.set('unique-id', gstore);


const bcrypt = require('bcrypt');
 
// Retrieve the gstore instance
const ggstore = instances.get('unique-id');
const { Schema } = ggstore;
 
/**
 * A custom validation function for an embedded entity
 */
const validateAccessList = (value, validator) => {
    if (!Array.isArray(value)) {
        return false;
    }
 
    return value.some((item) => {
        const isValidIp = !validator.isEmpty(item.ip) && validator.isIP(item.ip, 4);
        const isValidHostname = !validator.isEmpty(item.hostname);
 
        return isValidHostname && isValidIp;
    });
}
 
//Create the schema for the User Model
const userSchema = new Schema({
    firstname: { type: String, required: true },
    lastname: { type: String, optional: true  },
    email: { type: String, validate: 'isEmail', required: true },
    password: { type: String, read: false, required: true },
    createdOn: { type: String, default: gstore.defaultValues.NOW, write: false, read: false }

});

/**
 * List entities query shortcut
 */
const listSettings = {
    limit: 15,
    order: { property: 'lastname' }
};
userSchema.queries('list', listSettings);
 
/**
 * Pre "save" middleware
 * Each time the entity is saved or updated, if there is a password passed, it will be hashed
*/
function hashPassword() {
    // scope *this* is the entity instance
    const _this = this;
    const password = this.password;
 
    if (!password) {
        return Promise.resolve();
    }
 
    return new Promise((resolve, reject) => {
        bcrypt.genSalt(5, function onSalt(err, salt) {
            if (err) {
                return reject(err);
            };
 
            bcrypt.hash(password, salt, null, function onHash(err, hash) {
                if (err) {
                    // reject will *not* save the entity
                    return reject(err);
                };
 
                _this.password = hash;
 
                // resolve to go to next middleware or save method
                return resolve();
            });
        });
    });

 
// add the "pre" middleware to the save method
userSchema.pre('save', hashPassword);
 
/**
 * Export the User Model
 * It will generate "User" entity kind in the Datastore
*/
module.exports = gstore.model('User', userSchema);

迈克尔·吉迪恩

*我认为用户模型有问题**

您应该在 /models/user.js 中有一个像这样的 User 模型(将模型放在应用程序的根目录中)来定义 User:

const { instances } = require('gstore-node');
const bscrypt = require('bcrypt-nodejs');

// Retrieve the gstore instance
const gstore = instances.get('unique-id');
const { Schema } = gstore;

var usersSchema = new Schema({
    firstname:{type:String},
    lastname:{type:String},
    email:{type:String},
    password :{type:String},
    createdOn: Date
})

var User = gstore.model('User', usersSchema);

module.exports = User;

你忘了用 save() 保存

var newUser = new User ({
        firstname:req.body.name,
        lastname:req.body.name,
        email :req.body.Email,
        password :req.body.Password,
        createdOn: new Date() // there is a problem here.... use new Date()
      });

newUser.save(); //<======= it is abscent so it won't save 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

哪些Google Place API查询结果可以存储在数据库中?

来自分类Dev

在mysql数据库中存储等级

来自分类Dev

提交的数据未存储在数据库中

来自分类Dev

单选按钮值未存储在数据库中

来自分类Dev

Google Form数据存储在云SQL数据库中

来自分类Dev

表单字段未存储在mysql数据库中

来自分类Dev

Double未正确存储在MySQL数据库中

来自分类Dev

无法显示来自存储在mysql数据库中的坐标的google maps android标记

来自分类Dev

Google CloudML Serving是否允许连接到NoSQL数据库(例如数据存储区)?

来自分类Dev

在MongoDB数据库中存储图像

来自分类Dev

输入未存储在Mysql数据库中

来自分类Dev

在数据库中存储ID

来自分类Dev

数据库中的存储过程问题

来自分类Dev

无法在yii中的数据库中存储数据

来自分类Dev

日期返回结果错误,即使日期未存储在数据库中

来自分类Dev

在数据库中存储CSS

来自分类Dev

将临时结果存储在单独的数据库表中

来自分类Dev

提交的数据未存储在数据库中

来自分类Dev

我的数据未存储在数据库中

来自分类Dev

存储过程未更新数据库

来自分类Dev

如何使用Google课堂API在我们的SQL数据库中存储Google课堂作业和评估数据

来自分类Dev

在数据库中存储逻辑

来自分类Dev

解析数据库中的存储过程

来自分类Dev

如何将数据库中存储的多边形绘制到Google地图上

来自分类Dev

表单字段未存储在mysql数据库中

来自分类Dev

用户未存储在Spring数据库中

来自分类Dev

数据未存储到领域数据库中

来自分类Dev

在数据库中存储数组

来自分类Dev

从 Google GitHub 存储库加载 JSON 数据