正确地使用猫鼬将数据推入数据库时​​无法插入数据

时间:2019-07-14 05:08:16

标签: node.js mongodb express graphql

    createEvent: args => {
            const event = new Event({
                title: args.eventInput.title,
                description: args.eventInput.description,
                price: +args.eventInput.price,
                date: new Date(args.eventInput.date),
                creator: "5d2a29a0b17bb6472a4d0903"
            });
            let createdEvent;
            return event
            .save()
            .then(result => {
                createdEvent = {...result._doc, _id: result._doc._id.toString()};
                return user.findById("5d2a29a0b17bb6472a4d0903")
            })
            .then(user => {
                if(!user) {
                    throw new Error('User not found!')
                }
                // here i am printing to see event log
                console.log(event)
                user.createdEvents.push(event)
                return user.save() 
            })
            .then(result => {
                return createdEvent;
            })
            .catch(err => {
                console.log(err)
                throw err;
            });
        }

事件控制台日志

    { _id: 5d2ab75eae250925870b7971,
        title: 'hello',
        description: 'world',
        price: 5,
        date: 2019-07-14T04:04:10.313Z,
        __v: 0 }

在这里,我使用mongoose将字符串数据发送到mongodb,但未存储 与另一个收藏有关系的创作者。

我正在记录事件以查看内部即将发生什么,但是创建器未出现。creator

请看看

0 个答案:

没有答案