【问题标题】:Send to view.....information from two different databases发送以查看.....来自两个不同数据库的信息
【发布时间】:2019-08-06 18:27:58
【问题描述】:

下面是我的代码,

我正在尝试使用来自两个不同服务器的信息并将其用于名为 post 的视图。

我可以成功使用数据库“汽车”中的所有信息

但我无法从数据库“House”中提取信息

在帖子视图上 我这样调用汽车数据库:{{post.detail}}

对于housedatabase {{post2.detail}}

const Car = require('../database/models/Car') 
const House = require('../database/models/House')

module.exports = async (req, res) => {

const post = await Car.findById(req.params.id)

 const post2 = await House.find({})

        res.render('post', {

                post,
                post2,

        })

}

【问题讨论】:

  • 在控制台打印post2的值会得到什么?
  • [ { createdAt: 2019-03-15T05:06:23.673Z, _id: 5c8b32e39c580a3d34c6e696, detail: '/posts/house (3).mp3', __v: 0 },
  • post2 是一个对象数组,而不是一个对象。试试post2[0].detail
  • wooooooooooooooooooooooo @TGrif 成功了ddddddddd
  • @TGrif 你将如何遍历数组中的每个对象?如果不知道数组的大小?

标签: javascript html node.js mongoose


【解决方案1】:

作为@TGrif mentions in the comments

post2 是一个对象数组,而不是一个对象。试试post2[0].detail

这行得通 -> post2[0].detail

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2018-01-12
    • 2013-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多