简体   繁体   English

保存Node Js之前发送数据

[英]Sending data before saving Node Js

I have this function with a Post that I want to insert the data in the database (MongoDB). 我要发布此功能,我想将数据插入数据库(MongoDB)中。 First of all, I want to save the data in the model "Recetas" and after in the model "Users". 首先,我想将数据保存在“ Recetas”模型中,然后保存在“ Users”模型中。 And finally send the response. 最后发送响应。 My problem is that is not saving in the model Users and is sending the data before saving. 我的问题是没有保存在用户模型中,而是在保存之前发送数据。

This is my code 这是我的代码

addReceta = function (req, res) {

        User.findById(req.params.id, function(err, user){
            if (!user){
                res.send(404, 'User not found');
            }
            else{
                fs.readFile(req.files.file.path, function (err, data) {
                    var id = crypto.randomBytes(16).toString("hex");
                    var newPath = pwd + id +req.files.file.originalFilename;
                        fs.writeFile(newPath, data, function (err) {
                            var date_created = new Date();
                            var date  = date_created.toISOString().slice(0,10);
                            var receta = new Receta({
                                Titulo:  datos2.titulo,
                                Username:  datos2.username,
                                Ingredientes:  datos2.ingredientes,
                                Dificultad:  datos2.dificultad,
                                Descripción: datos2.descripcion,
                                Personas:  datos2.personas,
                                Date_Created: date_created,
                                Tiempo:  datos2.tiempo,
                                user_id: req.params.id,
                                imageUrl: URL + id + req.files.file.originalFilename
                            })
                            receta.save(function(err) {
                                if(!err) {
                                    console.log('Created in Receta');
                                } 
                                else {
                                    console.log(error);
                                }
                            });
                            var UserReceta = ({
                                _id: receta._id,
                                Titulo : receta.Titulo,
                                Username : receta.Username,
                                Descripción : receta.Descripción,
                                Ingredientes : receta.Ingredientes,
                                Dificultad : receta.Dificultad,
                                Personas : receta.Personas,
                                Date_Created: date_created,
                                Tiempo : receta.Tiempo,
                                user_id : receta.user_id,
                                imageUrl: receta.imageUrl
                            });
                            user.Recetas.push(UserReceta);
                            user.save(function (err){
                                if (!err) {
                                    console.log('Created in User Recetas');
                                } 
                                else {
                                    res.send(500, err);
                                }
                            });
                            res.send(receta);
                        });
                });
            };
        });
    };

Put your res.send(receta); 把你的res.send(receta); inside the callback after saving in Users, right after the console.log: 在保存到Users之后的回调中,就在console.log之后:

console.log('Created in User Recetas');
res.send(receta);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM